【问题标题】:SQLite-net Query not returning rowsSQLite-net 查询不返回行
【发布时间】:2017-11-30 00:16:05
【问题描述】:

我正在使用 xamarin 创建一个 Android 应用程序。我有一个预先存在的 sqlite 数据库,我正在加载到设备上。

我正在使用 Frank Kreuger 的 Sqlite-Net nuget 包。

在 db 中,我有一个表名为 Lists,其中当前有一行。

我正在使用 Query 方法来尝试检索该行。但它没有被退回。

我打开连接

db = new SQLiteConnection(FileAccessHelper.GetDBLocation(dbName));

这让我可以连接到位于我的数据库 数据库路径 "/data/user/0/readdb.readdb/files/packinglists.sqlite"

然后我尝试检索行

try
{
    List<tabledefs.Lists> myLists = db.Query<tabledefs.Lists>("select * from Lists");
    foreach (tabledefs.Lists list in myLists)
    {
        listNames[counter++] = list.ListName;
    }
}catch (Exception e)
{
    //do something
}

即使我可以手动打开数据库并查询数据,该调用也不会返回任何行。

【问题讨论】:

    标签: c# sqlite xamarin.android


    【解决方案1】:

    并没有真正在移动设备上摆弄过 SQLite,但可以肯定该过程与 Web 服务相同。

    // Location of the Database
    string connectionString;
    
    // Use a "Using" instead of a try-catch-finally statement for automatic Dispose
    using (var db = new SQLiteConnection (connectionString))
    {
       // Use a Data Adapter and CommandBuilder to initiate and receive the data from the db.
       SQLiteDataAdapter myDataAdapter = new SQLiteDataAdapter ("select * from Lists", connectionString);
       SQLiteCommandBuilder myCommand = new SQLiteCommandBuilder (myDataAdapter);
    
       // Create a Data Table to Store the db
       // Open the Database
       // Fill the Data Table from the Adapter that received the db
       DataTable newDataTable = new DataTable;
       db.Open();
       myDataAdapter.Fill (newDataTable);
    
       // Now if you run through each row and use "row.ItemArray[]" you can get 
       // the specific data type in each value.
       foreach (DataRow row in newDataTable.Rows)
       {
           "Your Rows Here"
       }
    }
    

    应该给你单独的每一行你所要做的就是将该行的每个索引转换为正确的值类型以获取列的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多