如何通过SQLiteAsyncConnection执行原始SQLite查询(具有多行结果) [英] How to perform raw SQLite query (With multiple row result) through SQLiteAsyncConnection

查看:510
本文介绍了如何通过SQLiteAsyncConnection执行原始SQLite查询(具有多行结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do not have a class map to a table. I'm referring to the following thread. The get the number of row in a table, I am applying the following technique

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

SQLiteAsyncConnection conn = new SQLiteAsyncConnection(DATABASE_NAME);
int profileCount = await conn.ExecuteScalarAsync<int>("select count(*) from " + PROFILE_TABLE);

现在,我想获取多个行数据中的结果,而不是获取作为行数的结果.

Now, instead of obtaining result as number of row, I would like to retrieve result in multiple row data.

在Java中,我将执行以下操作以获取多行结果数据

In Java, to obtain multiple row result data, I would perform

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来实现?

Given a same sql statement, how can I achieve using SQLiteAsyncConnection?

推荐答案

我在SQLite.cs中添加了2个新函数.不优雅,但是对我有用.

I added 2 new functions into SQLite.cs. Not elegant, but it works for me.

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

这篇关于如何通过SQLiteAsyncConnection执行原始SQLite查询(具有多行结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆