执行更新返回1但不更新

时间:2015-10-28 18:23:15

标签: java sql sqlite

我正在尝试更新我的本地sqlite表。 我的executeUpdate()返回1但不更新。 我无法添加完整的可运行代码,因为它需要使用本地数据库。

这是我更新表的代码块。

 protected int notGuncelle(int gelenId, String yeniNot) {
    int update = -1;

    try {
        update = st.executeUpdate("UPDATE notlar SET 'not' = " + "'yeniNot'"  + " WHERE id = " + gelenId);

        System.out.println(update);
    } catch (SQLException ex) {
        //Logger.getLogger(SQLiteFunctions.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Not güncelleme hatası: " + ex.getLocalizedMessage());
    }

    return update;

}

现在下面的代码返回-1但是有效。我真的很困惑。我认为这是因为将yeniNot变量传递给查询的方式。那里有些不对劲但我不知道是什么。

protected int notGuncelle(int gelenId, String yeniNot) {
    int update = -1;
    System.out.println(yeniNot);
    try {
        PreparedStatement st = this.conn.prepareStatement("UPDATE notlar SET 'not' = ? WHERE id = ?");
        st.setString(1, yeniNot);
        st.setString(2, "" + gelenId);

        st.executeUpdate();
        //update = st.executeUpdate("UPDATE notlar SET 'not' = " + yeniNot + " WHERE id = " + gelenId);
        System.out.println(update);
    } catch (SQLException ex) {
        //Logger.getLogger(SQLiteFunctions.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Not güncelleme hatası: " + ex.getLocalizedMessage());
    }

    return update;

}

2 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。

必须更换

 update = st.executeUpdate("UPDATE notlar SET 'not' = " + "'yeniNot'"  + " WHERE id = " + gelenId);

用这个

update = st.executeUpdate("UPDATE notlar SET 'not' = + '"+yeniNot+"'" + " " + "WHERE id = " + gelenId);

"'yeniNot'"是将String参数传递给查询的错误样式 它应该是>> '"+yeniNot+"'

答案 1 :(得分:0)

如果您已将autocommit设置为false

        con.setAutoCommit(false);

然后你必须通过调用方法

手动提交
        con.commit();

当然,您可以将autocommit设置为true,然后将每个语句视为单个事务

相关问题