我没有表格的地图。我指的是以下帖子。获取表中的行数,我正在应用以下技术
How to perform raw SQLite query through SQLiteAsyncConnection
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(DATABASE_NAME);
int profileCount = await conn.ExecuteScalarAsync<int>("select count(*) from " + PROFILE_TABLE);
现在,我不想将结果作为行数获取,而是希望在多行数据中检索结果。
在Java中,为了获得多行结果数据,我会执行
Cursor cursor = database.rawQuery(sql, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
// For every cursor, obtain its col data by
// cursor.getLong(0), cursor.getInt(1), ...
cursor.moveToNext();
}
给定相同的sql语句,如何使用SQLiteAsyncConnection
实现?
答案 0 :(得分:1)
我在SQLite.cs中添加了2个新函数。不优雅,但它对我有用。
// Invented by yccheok :)
public IEnumerable<IEnumerable<object>> ExecuteScalarEx()
{
if (_conn.Trace)
{
Debug.WriteLine("Executing Query: " + this);
}
List<List<object>> result = new List<List<object>>();
var stmt = Prepare();
while (SQLite3.Step(stmt) == SQLite3.Result.Row)
{
int columnCount = SQLite3.ColumnCount(stmt);
List<object> row = new List<object>();
for (int i = 0; i < columnCount; i++)
{
var colType = SQLite3.ColumnType(stmt, i);
object val = ReadColEx (stmt, i, colType);
row.Add(val);
}
result.Add(row);
}
return result;
}
// Invented by yccheok :)
object ReadColEx (Sqlite3Statement stmt, int index, SQLite3.ColType type)
{
if (type == SQLite3.ColType.Null) {
return null;
} else {
if (type == SQLite3.ColType.Text) {
return SQLite3.ColumnString (stmt, index);
}
else if (type == SQLite3.ColType.Integer)
{
return (int)SQLite3.ColumnInt (stmt, index);
}
else if (type == SQLite3.ColType.Float)
{
return SQLite3.ColumnDouble(stmt, index);
}
else if (type == SQLite3.ColType.Blob)
{
return SQLite3.ColumnBlob(stmt, index);
}
else
{
throw new NotSupportedException("Don't know how to read " + type);
}
}
}