SQLlite Update查询总是在android项目中返回0

时间:2016-06-04 17:04:31

标签: c# android sqlite xamarin

我正在尝试按下按钮更新数据库。但它总是返回0,并且数据库不会更新。

string query4 = "update Praysettings set value ='" + LocName + "' where id =52;";

db2.ExecuteQuery(query4);

public void ExecuteQuery(string query) 
{
    string dbPath = Path.Combine (
        System.Environment.GetFolderPath (
            System.Environment.SpecialFolder.Personal
        ),
        "tzmpo_t.rar"
    );

    if (File.Exists(dbPath)) {
        using (var db = new SQLiteConnection(dbPath))
        {
            int a = db.Execute(query);
            db.Close();
        } 
    }
}

1 个答案:

答案 0 :(得分:0)

价值已经存在?如果您没有更新该值,则不会受到影响的行。检查是否是这种情况。

以下是我使用的代码,可能对您有所帮助:

    private void OpenConnection()
    {
        lock (syncLock)
        {
            if (_connection == null || _connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken)
            {
                _connection = new SqliteConnection("Data Source=" + DbPath);
                _connection.SetPassword(DbKey);
                _connection.Open();
            }
        }
    }

    private int ExecuteNonQuery(string query)
    {
        lock (syncLock)
        {
            OpenConnection();
            int rowcount = 0;
            using (var command = _connection.CreateCommand())
            {
                command.CommandText = query;
                try
                {
                    rowcount = command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Logger.Error("ExecuteNonQuery", e.Message + ". Query:" + query);
                }
                command.Dispose();
            }

            return rowcount;
        }
    }