1

我没有表的类映射。我指的是以下线程。获取表中的行数,我正在应用以下技术

如何通过 SQLiteAsyncConnection 执行原始 SQLite 查询

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 语句,我该如何实现 using SQLiteAsyncConnection

4

1 回答 1

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);
            }
        }
    }
于 2012-12-05T03:28:25.620 回答