如何使用 SQLite-Net Extensions 进行低 level/plain SQL 查询?
How to make low level/plain SQL queries with SQLite-Net Extensions?
我正在使用 SQLite-Net Extensions,我想在对它进行一些操作之前检查 table 是否存在。这是我试过的:
await this.database.ExecuteScalarAsync<int>("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='{0}'", tableName).ContinueWith( t =>
{
System.Diagnostics.Debug.WriteLine(t.Result);
});
输出为0
。如果我直接在数据库上查询它,我会得到一个结果 (1
)。然后我试了
var tableQuery = string.Format("SELECT name FROM sqlite_master WHERE type='table' AND name='{0}'", tableName);
var result = await this.database.ExecuteAsync(tableQuery);
我得到一个例外:
SQLite.SQLiteException: Row
at SQLite.SQLiteCommand.ExecuteNonQuery () [0x0009b] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:2172
at SQLite.SQLiteConnection.Execute (System.String query, System.Object[] args) [0x00040] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:659
at SQLite.SQLiteAsyncConnection+<ExecuteAsync>c__AnonStorey9.<>m__0 () [0x00013] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLiteAsync.cs:222
at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <d18287e1d683419a8ec3216fd78947b9>:0
at System.Threading.Tasks.Task.Execute () [0x00010] in <d18287e1d683419a8ec3216fd78947b9>:0
at App.Database+<ClearTable>d__63.MoveNext () [0x00069] in xxx\Database.cs:1011
at App.Util.Helper+<CleanUpTemporaryFiles>d__8.MoveNext () [0x0013a] in xxx\Helper.cs:200
at App.Pages.DetailPage+<OnDisappearing>d__23.MoveNext () [0x000b1] in xxx\DetailPage.xaml.cs:449
对于 QueryAsync
你需要一个带有无参数构造函数的对象。
现在我不知道如何进行简单的 SELECT
查询。是否还有一种方法可以获得比 Row
更详细的异常消息?
您将参数传递给 SQL 的方式不正确。根据 the docs,而不是
AND name='{0}'
你应该使用:
name = ?
我正在使用 SQLite-Net Extensions,我想在对它进行一些操作之前检查 table 是否存在。这是我试过的:
await this.database.ExecuteScalarAsync<int>("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='{0}'", tableName).ContinueWith( t =>
{
System.Diagnostics.Debug.WriteLine(t.Result);
});
输出为0
。如果我直接在数据库上查询它,我会得到一个结果 (1
)。然后我试了
var tableQuery = string.Format("SELECT name FROM sqlite_master WHERE type='table' AND name='{0}'", tableName);
var result = await this.database.ExecuteAsync(tableQuery);
我得到一个例外:
SQLite.SQLiteException: Row
at SQLite.SQLiteCommand.ExecuteNonQuery () [0x0009b] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:2172
at SQLite.SQLiteConnection.Execute (System.String query, System.Object[] args) [0x00040] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:659
at SQLite.SQLiteAsyncConnection+<ExecuteAsync>c__AnonStorey9.<>m__0 () [0x00013] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLiteAsync.cs:222
at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <d18287e1d683419a8ec3216fd78947b9>:0
at System.Threading.Tasks.Task.Execute () [0x00010] in <d18287e1d683419a8ec3216fd78947b9>:0
at App.Database+<ClearTable>d__63.MoveNext () [0x00069] in xxx\Database.cs:1011
at App.Util.Helper+<CleanUpTemporaryFiles>d__8.MoveNext () [0x0013a] in xxx\Helper.cs:200
at App.Pages.DetailPage+<OnDisappearing>d__23.MoveNext () [0x000b1] in xxx\DetailPage.xaml.cs:449
对于 QueryAsync
你需要一个带有无参数构造函数的对象。
现在我不知道如何进行简单的 SELECT
查询。是否还有一种方法可以获得比 Row
更详细的异常消息?
您将参数传递给 SQL 的方式不正确。根据 the docs,而不是
AND name='{0}'
你应该使用:
name = ?