【问题标题】:Why SQLiteConnection.Update(client) doesn't Update a table entry?为什么 SQLiteConnection.Update(client) 不更新表条目?
【发布时间】:2021-12-22 22:46:15
【问题描述】:

我正在解决一个运行良好的 Android 应用程序中的错误,但是当我更新客户端时,它不会在数据库中进行更改。我正在构建一个查询,例如:

            StringBuilder query= new StringBuilder("PRAGMA encoding = \"UTF-8\"; UPDATE tclientes SET");
            //Building the query...
            query.AppendFormat(" WHERE cli_code = '{0}';", cliente.cli_code);
            Db.Query<tclientes>(query.ToString()); //this in a try-catch

它不会捕获任何错误,也不会更新条目。我也试过:

            Db.Update(cliente);

for 'client 是模型的一个实例,但这会触发错误:“System.NotSupportedException:无法更新客户端:它在 SQLite.SQLiteConnection.Update 处没有 PK”,可能是用作键的值(cli_code)是不足够的?我不知道该怎么办

编辑: 我也尝试过创建一个 SQLiteCommand 和绑定参数,但没有结果。它适用于“InsertOrUpdate(client)”,这很棒,但我仍然没有主要问题的答案

【问题讨论】:

  • 表是如何定义的?第二条错误消息非常清楚地告诉您该表缺少 PK

标签: c# android visual-studio sqlite xamarin


【解决方案1】:

更新:

创建表和插入、更新数据库可以参考下面的代码。

var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
        var db = new SQLiteConnection(dbpath);
        db.CreateTable<Employee>();
        db.CreateTable<Duty>();
        var employee = new Employee
        {
            Name = "Andrew",
            LastName = "Programmer"
        };
        db.Insert(employee);


        var duty1 = new Duty()
        {
            Description = "Project A Management",
            Deadline = new DateTime(2017, 10, 31)
        };

        var duty2 = new Duty()
        {
            Description = "Reporting work time",
            Deadline = new DateTime(2022, 12, 31)
        };

        db.Insert(duty1);
        db.Insert(duty2);

        employee.Duties = new List<Duty> { duty1, duty2 };
        db.UpdateWithChildren(employee);

【讨论】:

  • 这适用于异步连接,但我有一个单音 SQLiteConnection,我不能这样称呼它
  • 我更新了我的回复。请检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 2018-07-06
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
相关资源
最近更新 更多