如何通过SQLiteAsyncConnection执行原始SQLite的查询(用多行结果)(How

2019-10-17 11:22发布

我没有一类映射到表。 我指的是以下螺纹。 在得到行的一个表中的号码,我申请了以下技术

如何执行通过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语句,我如何能实现使用SQLiteAsyncConnection

Answer 1:

我加了2个新的功能整合到SQLite.cs。 不优雅,但它为我工作。

    // 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);
            }
        }
    }


文章来源: How to perform raw SQLite query (With multiple row result) through SQLiteAsyncConnection