2012-12-03 19 views
1

我没有到表的类映射。我指的是以下线程。将得到表行的数量,我申请了以下技术如何通过SQLiteAsyncConnection执行原始SQLite查询(具有多行结果)

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

回答

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