【问题标题】:executenonquery not working, no columns editedexecutenonquery 不工作,没有编辑列
【发布时间】:2017-02-18 18:22:13
【问题描述】:

我正在尝试更新我的数据库。但是,当我打印 ExecuteNonQuery() 返回的对象时,它会显示 0。

这是我的代码:

try
        {
            using(dbConnection = new SqliteConnection(dbConnString))
            {
                dbConnection.Open();
                using(dbCommand = dbConnection.CreateCommand())
                {
                    dbCommand.CommandText = "UPDATE 'GameObject' SET 'LocationX' = @locX, 'LocationY' = @locY, 'LocationZ' = @locZ WHERE 'id' = @id";
                    dbCommand.Parameters.AddRange(new SqliteParameter[]
                    {
                        new SqliteParameter("@locX") { Value = x},
                        new SqliteParameter("@locY") { Value = y},
                        new SqliteParameter("@locZ") { Value = z},
                        new SqliteParameter("@id") {Value = id}

                    });
                    int i = dbCommand.ExecuteNonQuery();
                    Debug.Log(i);
                }//end dbcommand
            }//end dbconnection
        }//end try
        catch(Exception e)
        {
            Debug.Log(e);
        }

我认为这是因为我的 where 子句,因为当我将此子句添加到选择查询时,我的变量不会得到更新。我只是找不到它有什么问题。

选择查询:

try
        {
            using(dbConnection = new SqliteConnection(dbConnString))
            {
                dbConnection.Open();
                using(dbCommand = dbConnection.CreateCommand())
                {
                    dbCommand.CommandText = "SELECT * FROM 'GameObject' WHERE 'id' = @id"; //without the where everything works fine
                    dbCommand.Parameters.Add (new SqliteParameter("@id") { Value = 1});
                    using(dbReader = dbCommand.ExecuteReader())
                    {
                        while(dbReader.Read())
                        {
                            id = dbReader.GetInt32(0);
                            x = dbReader.GetFloat(1);
                            y = dbReader.GetFloat(2);
                            z = dbReader.GetFloat(3);
                        }
                    }//end dbreader
                }//end dbcommand
            }//end dbconnection

            _object.transform.position = new Vector3(x,y,z);
            Debug.Log ("id: " + id + " location: " + _object.transform.position);
        }
        catch(Exception e)
        {
            Debug.Log(e);
        }

【问题讨论】:

  • 此查询是否适用于您的数据库管理器?能不能说的具体点?
  • 对不起,如果我将 where 添加到我的选择查询中,我的变量不会得到更新。我也会发布我的选择的代码。
  • @SonerGönül 不,它在经理中也不起作用。我不明白,因为我在数据库中只有 1 个项目,它的 id 是 1
  • @Aelion 在您的int i = dbCommand.ExecuteNonQuery(); 行上设置一个断点,调试您的代码并告诉您的dbCommand 到底长什么样。

标签: c# sqlite


【解决方案1】:

一个原因可能来自https://www.sqlite.org/lang_keywords.html

为了应对历史 SQL 语句时的弹性,SQLite 有时会违反上述引用规则:

如果单引号中的关键字(例如:'key''glob')用于 允许标识符但字符串文字所在的上下文 不允许,则令牌被理解为标识符 字符串文字。

由于它们都不是关键字,因此请尝试在不使用单引号的情况下使用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2013-12-17
    • 2016-08-15
    • 1970-01-01
    • 2015-10-25
    相关资源
    最近更新 更多