我有这个课:
public class BtnCountViews
{
public int btnCount { get; set; }
public int views { get; set; }
}
我正在尝试从数据库中检索一行数据:
sql = " AVG (BTNACOUNT + BTNBCOUNT) AS btnCount, AVG (VIEWS) As views ";
var avg = db2.ExecuteScalar<BtnCountViews>("SELECT " + sql + " FROM CLICKHISTORY");
但这引发了异常的说法:
System.NotSupportedException: Don't know how to read Japanese.BtnCountViews
at SQLite.SQLiteCommand.ReadCol (SQLitePCL.sqlite3_stmt stmt,
我试图研究ExecuteScalar,看来它可以带一个对象。
有谁知道为什么它会引发异常?
作为参考,下面是CLICKHISTORY表的对象定义:
public class ClickHistory
{
[PrimaryKey, NotNull]
public string Yymmdd { get; set; }
public int DayOfYear { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int BtnACount { get; set; }
public int BtnBCount { get; set; }
public int BtnCCount { get; set; }
public int BtnDCount { get; set; }
public int Views { get; set; }
}
最佳答案
您正在尝试返回BtnCountViews
类型,而不是标量值。
通过BtnCountViews
返回投影的Query
:
var sql = "AVG (BTNACOUNT + BTNBCOUNT) AS btnCount, AVG (VIEWS) As views ";
var avg = conn.Query<BtnCountViews>("SELECT " + sql + " FROM CLICKHISTORY").First();
Log.Debug(TAG, $"{avg.btnCount} : {avg.views}");
或通过
ExecuteScalar
在两个单独的查询中带回标量:var sql2 = "(AVG (BTNACOUNT + BTNBCOUNT))";
var btnABAvg = conn.ExecuteScalar<int>("SELECT " + sql2 + " FROM CLICKHISTORY");
var sql3 = "(AVG (VIEWS))";
var viewAvg = conn.ExecuteScalar<int>("SELECT " + sql3 + " FROM CLICKHISTORY");
Log.Debug(TAG, $"{btnABAvg} : {viewAvg}");
关于sqlite - 尝试使用ExecuteScalar <object>从表中检索数据时出错?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47676706/