【问题标题】:Xamarin forms trouble updating Boolean with SQLiteXamarin 使用 SQLite 更新布尔值时出现问题
【发布时间】:2016-08-10 10:55:44
【问题描述】:

我正在尝试使用 Xamarin 表单创建一个简单的 Notes 应用程序。我有条目,一个日期,然后是一个开关来确定笔记是否完成。

创建新笔记时,插入数据一切正常。但是当我尝试更新注释时,开关元素始终插入为“false”,其他数据适用于更新。我不知道问题可能是什么?

更新笔记的方法如下:

 public void UpdateNote(string _name, string _note, string _date, bool _done, int noteId)
    {
        _sqlconnection.Query<Notes>("UPDATE [Notes] SET Name='" + _name + "', Note='" + _note + "', Date='" + _date + "', Done='" + _done + "' WHERE [ID]='" + noteId + "'");
    }

下面是 Views 代码背后的点击事件:

 UpdateClick.Clicked += delegate {
            _notes = new Notes();
            _notesDb = new NotesDB();
            _notes.Name = name.Text;
            _notes.Note = note.Text;
            _notes.Date = DateTime.Now.ToString();
            _notes.Done = done.IsToggled;
            _notesDb.UpdateNote(_notes.Name, _notes.Note, _notes.Date, _notes.Done, noteId);
            Navigation.PopAsync();
        };

【问题讨论】:

    标签: c# sqlite xaml xamarin xamarin.forms


    【解决方案1】:

    SQLite 将布尔值存储为 0/1:

    var _doneInt = _done : 1 : 0;
    

    public void UpdateNote(string _name, string _note, string _date, bool _done, int noteId)
    {
        var _doneInt = _done : 1 : 0;
        _sqlconnection.Query<Notes>("UPDATE [Notes] SET Name='" + _name + "', Note='" + _note + "', Date='" + _date + "', Done='" + _doneInt + "' WHERE [ID]='" + noteId + "'");
    }
    

    1.1 布尔数据类型

    SQLite 没有单独的布尔存储类。相反,布尔值存储为整数 0(假)和 1(真)。

    参考:https://www.sqlite.org/datatype3.html

    【讨论】:

      【解决方案2】:

      SQLite 中没有布尔类型,因此您必须使用 0 表示假,使用 1 表示真。此外,您应该使用 Execute,而不是使用 Query

      _sqlconnection.Execute("UPDATE [Notes] SET Name='" + _name + "', Note='" + _note + "', Date='" + _date + "', Done='" + _done ? 1 : 0 + "' WHERE [ID]='" + noteId + "'");
      

      来自SQLite documentation:

      SQLite 没有单独的布尔存储类。相反,布尔值存储为整数 0(假)和 1(真)。

      【讨论】:

      • 哦!现在更有意义了!谢谢!虽然上面的格式似乎不适合我?
      • 我想通了。这就是我所做的:'int _doneInt = _done ? 1 : 0;´ 感谢大家的帮助! :Dcode
      【解决方案3】:

      你不能只使用更新吗? 发送对象进行更新,然后让 sqlite.net 解决:

      public void UpdateNote(Notes _note)
      {
          _sqlconnection.Update(_note)
      }
      

      如果您查看here 中的更新测试调用 这是假设您的 noteId 字段具有 [PrimaryKey]

      这似乎对我有用:

      型号:

          public class Notes
          {
              [PrimaryKey, AutoIncrement, NotNull] 
              public int Id { get; set; } 
              public string Name { get; set; } 
              public string Note { get; set; } 
              public string Date { get; set; } 
              public bool Done { get; set; } 
          }
      

      测试方法:

          public bool testForNotes(Notes n)
          {
              _sqliteConnection.CreateTable<Notes>();
              _sqliteConnection.Insert (n);
              n.Done = true;
              var i = _sqliteConnection.Update (n);
              return (i > 0);
          }
      

      单元测试

          public void GetNotes ()
          {
              NoteDataStore _noteDataStore = new NoteDataStore (new SqlConnection());
              var note = new Notes () {
                  Name = "TestName",
                  Note = "TestNote",
                  Date = DateTime.Now.ToString (),
                  Done = false
              };
              var show = _noteDataStore.testForNotes (note);
              Assert.IsNotNull (show);
          }
      

      【讨论】:

      • 我已经尝试过了,但由于某种原因它对我不起作用:/
      • 我知道上面的答案解决了你的问题,但你能发布你的 Notes 模型吗?由于您无法执行插入或其他 sqlite 命令,因此必须手动进行 SQLite 查询
      • 当然!这是我的模型:[PrimaryKey, AutoIncrement, NotNull] public int ID { get; set; } public string Name { get; set; } public string Note { get; set; } public string Date { get; set; } public bool Done { get; set; } public Notes() { } 对于插入,我使用 SQLites 插入方法,所以我真的很困惑为什么不能使用更新。
      • 在应用程序输出中使用更新是否有任何错误?
      猜你喜欢
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2020-11-29
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多