【问题标题】:SQLite table doesn't get updatedSQLite 表未更新
【发布时间】:2019-10-27 07:43:22
【问题描述】:

我有问题。我创建了一个 SwitchButton 并希望将状态存储在数据库表中。所以我创建了这段代码来调试:

SettingSwitch.CheckedChange += (s, b) =>
{
    SettingDb testsetting = new SettingDb
    {
        Name = mItems[position].Name,
    };

    SettingDb test = MainActivity.db.SelectRowFromTableSettings(testsetting);

    if (test != null)
    {
        bool SwitchValueBool = Convert.ToBoolean(test.Value);
    }


    bool isChecked = ValueDictionary[position];
    if(isChecked == true)
    {
        isChecked = false;
    }
    else if(isChecked == false)
    {
        isChecked = true;
    }

    SettingDb setting = new SettingDb()
    {
        Name = SettingName.Text,
        Type = "Switch",
        Value = isChecked.ToString()
    };

    MainActivity.db.UpdateTableSettings(setting);
    ValueDictionary[position] = isChecked;

    SettingDb test2 = MainActivity.db.SelectRowFromTableSettings(testsetting);

    if (test2 != null)
    {
        bool SwitchValueBool = Convert.ToBoolean(test2.Value);
    }
};

预期的结果应该是:

test.Value = False
test2.Value = Opposite of test.Value, so True

但是现在我从表中得到的值总是 False。这里是更新函数:

string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
public bool UpdateTableSettings(SettingDb setting)
{
    try
    {

        using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Settings.db")))
        {
            connection.BeginTransaction();
            connection.Query<SettingDb>("UPDATE SettingDb SET Value=? WHERE Name=?", setting.Value, setting.Name);
            //connection.Update(setting);
            connection.Commit();
            return true;
        }
    }
    catch (SQLiteException ex)
    {
        Log.Info("SQLiteEx", ex.Message);
        return false;
    }
}
public SettingDb SelectRowFromTableSettings(SettingDb setting)
{
    try
    {
        using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Settings.db")))
        {
            return connection.Query<SettingDb>("SELECT * FROM SettingDb WHERE Name=?", setting.Name).FirstOrDefault();
        }
    }
    catch (SQLiteException ex)
    {
        Log.Info("SQLiteEx", ex.Message);
        return null;
    }
}

表值没有更新!!! 有人可以告诉我我做错了什么吗? 请告诉我!

【问题讨论】:

    标签: c# sqlite xamarin.android android-sqlite


    【解决方案1】:

    根据您的描述,您想更新sqlite数据库表,请看下面的代码并修改更新函数。

     static void UpdateDatabase(int primaryKey, string newText, int newValue)
    {
        string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "mydatabase.db");
        var db = new SQLiteConnection(path, false);
        string sql = "UPDATE MyTable SET MyTextColumn = ?, MyValueColumn = ? WHERE MyPrimaryKey= ?";
        string[] parms = new String[] { newText, newValue.ToString(), primaryKey.ToString() };
        var cmd = db.CreateCommand(sql, parms);
        cmd.ExecuteNonQuery();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2021-06-22
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多