更新Sqlite数据库中的数据库记录

时间:2013-04-08 07:04:04

标签: sqlite windows-8

这里我在Windows 8中插入SQLite数据库代码,我想更新数据库中添加的记录

private async void insert(object sender, RoutedEventArgs e) {
  if (txt1.Text != "" && txt2.Text != "" && txt3.Text != "") {
    var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
    using (var db = new SQLite.SQLiteConnection(dbpath)) {
      // Create the tables if they don't exist
      db.Insert(new person() {
        id= Guid.NewGuid(),
        name = txt1.Text.ToString(),
        address = txt2.Text.ToString(),
        phone = Convert.ToDouble(txt3.Text.ToString()),
      });

      db.Commit();
      db.Dispose();
      db.Close();
    }
  } else {
    throw new NullReferenceException("Enter The Data In Textboxes");
  }
}

1 个答案:

答案 0 :(得分:0)

在SQLite中有Get<T>方法接受主键作为参数,它将该行作为对象返回。 Update方法接受对象作为参数,它将更新现有记录。在这里,我将为您提供更新人员记录的方法。

private void UpdateRecord(int primaryKey)
{
    var dbpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
    using (var db = new SQLite.SQLiteConnection(dbpath))
    {
        var objPerson = db.Get<person>(primaryKey);
        objPerson.name = "New name";
        objPerson.address = "New address ";
        objPerson.phone = Convert.ToDouble("New phone number");
        db.Update(objPerson);
        //You don't need to use db.Commit(), db.Dispose() & db.Close() because you are using "using" keyword.
    }
}