【问题标题】:sqlite-net-pcl {SQLite.SQLiteException: near ")": syntax errorsqlite-net-pcl {SQLite.SQLiteException:靠近“)”:语法错误
【发布时间】:2017-09-24 12:00:13
【问题描述】:

我目前正在使用 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 ()

这很清楚为什么它不起作用,但是如何生成这样的语句?我错过了什么还是一个错误?

更新:是的,我使用了搜索功能,但没有适合我的问题。

【问题讨论】:

  • service.GetSumPerpetrationParts(immobilePerpetrationId);返回什么类型?因为这就是您要插入的内容。

标签: c# ios sqlite xamarin.ios sqlite-net


【解决方案1】:

应该这样:

await con.InsertAsync(perpetrationPartCount);

是:

await con.InsertAsync(model);

我们无法根据您的代码判断 perpetrationPartCount 的类型。它可能不是PPPCount,因此该实体可能不是问题。

【讨论】:

  • 天哪,我不敢相信我试图将一个整数值插入到我的数据库中!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多