【问题标题】:How to perform raw SQLite query through SQLiteAsyncConnection如何通过 SQLiteAsyncConnection 执行原始 SQLite 查询
【发布时间】:2012-11-09 12:48:35
【问题描述】:

我指的是http://timheuer.com/blog/archive/2012/08/07/updated-how-to-using-sqlite-from-windows-store-apps.aspxhttp://wp.qmatteoq.com/using-sqlite-in-your-windows-8-metro-style-applications/

据我所知,sqlite-net 对外键没有很好的支持。因此,我没有创建一个类来映射到表,而是使用以下方式创建表。

    public static readonly String PROFILE_TABLE_CREATE = "create table if not exists " 
            + PROFILE_TABLE_NAME + "(" 
            + COLUMN_PROFILE_ID + " integer primary key autoincrement, "
            + COLUMN_TIMESTAMP + " integer not null, "
            + COLUMN_PROFILE_NAME + " text not null, "
            + COLUMN_AGE + " integer not null, "
            + COLUMN_GENDER + " integer not null"
            + ");";

    private async void CreateDatabase()
    {
        SQLiteAsyncConnection conn = new SQLiteAsyncConnection(DATABASE_NAME);
        await conn.ExecuteAsync(PROFILE_TABLE_CREATE);
    }

现在,我想执行查询。例如,特定表中有多少行?

我可以像read column names from sqlite table windows 8 app 一样执行QueryAsync。但是,我没有要映射到表格的类。

在 Java 中,我可以执行如下原始查询:

    String sql =  "select count(*) from " + tableName + " where " + HistorySQLiteOpenHelper.COLUMN_FK_TIMESTAMP + " = " + profileTimestamp;
    Cursor cursor = database.rawQuery(sql, null);

如何在 C# Windows Store App 中执行此操作?

【问题讨论】:

    标签: c# sqlite windows-8


    【解决方案1】:

    要获取单个值,例如计数,您可以使用 ExecuteScalarAsync

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

    【讨论】:

      猜你喜欢
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多