我目前正在使用 Xamarin 开发一个 iOS 应用程序,并在使用 sqlite-net-pcl 时遇到了一个奇怪的错误:
{SQLite.SQLiteException: near ")": syntax error at SQLite.SQLite3.Prepare2 (SQLitePCL.sqlite3 db, System.String query) [0x0001e] in <49ac49cfb94341128f6929b3ff2090ee>:0 at SQLite.PreparedSqlLiteInsertCommand.Prepare () [0x00011] in <49ac49cfb94341128f6929b…}
当我想插入以下模型的表时出现错误:
public class PPPCount
{
public PPPCount()
{
}
[PrimaryKey]
public int Id { get; set; }
public string PerpePartCount { get; set; }
}
调用代码如下:
try
{
var con = await DbFactory.Instance();
var perpetrationPartCount = await
service.GetSumPerpetrationParts(immobilePerpetrationId);
var dbModel = await con.FindAsync<PPPCount>(immobilePerpetrationId);
if (dbModel == null)
{
var model = new PPPCount();
model.Id = immobilePerpetrationId;
model.PerpePartCount = perpetrationPartCount;
//This causes the exception!!!!
await con.InsertAsync(perpetrationPartCount);
}
else
{
if (dbModel.PerpePartCount != perpetrationPartCount)
{
dbModel.PerpePartCount = perpetrationPartCount;
await con.UpdateAsync(dbModel);
}
}
return perpetrationPartCount;
}
catch (Exception e)
{
//AlertHelper.ShowError(e.Message);
}
创建和保存我的 sqlite 连接对象的 DbFactory 的代码:
public class DbFactory
{
private static SQLiteAsyncConnection connection;
public static async Task<SQLiteAsyncConnection> Instance()
{
if (connection == null)
{
bool deleteDb = false;
string folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var dbPath = System.IO.Path.Combine(folder, "..", "immotech_offline.db3");
if (File.Exists(dbPath) && deleteDb)
{
File.Delete(dbPath);
}
connection = new SQLiteAsyncConnection(dbPath);
try
{
await connection.CreateTableAsync<ImmobilePerpetration>();
await connection.CreateTableAsync<Customer>();
await connection.CreateTableAsync<PPPCount>();
}
catch (Exception e)
{
//TODO: Delete this part!
int i = 0;
}
}
return connection;
}
}
奇怪的是我可以毫无问题地使用其他两个模型! 我真的无法解释导致此错误的原因,我尝试启用跟踪以显示 SQL 语句,但不幸的是,sqlite 连接对象的异步版本不提供跟踪。
然后我在启用跟踪的同步版本中尝试了相同的代码,结果如下:
insert into "Int32"() values ()
嗯,这很清楚为什么它不起作用,但是如何生成这样的语句呢?我是否遗漏了什么或者这是一个错误?
更新:是的,我使用了搜索功能,但没有适合我问题的案例。
最佳答案
应该这样:
await con.InsertAsync(perpetrationPartCount);
成为:
await con.InsertAsync(model);
我们无法根据您的代码判断 perpetrationPartCount
是什么类型。它可能不是 PPPCount
,因此该实体可能不是问题所在。
关于c# - sqlite-net-pcl {SQLite.SQLiteException : near ")": syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645099/