为什么我不能从Python更新SQLite数据库中的数据?

2024-10-03 13:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新或修改flaskpython中SQLite数据库特定单元中的现有数据。但我不能更新。同时我也没有收到任何错误。同时,我可以在表的新行中插入新数据。在

服务器端代码:

@app.route("/")
@app.route("/<state>")
def get_post_javascript_data(state=None):
        connection = sqlite3.connect('/home/pi/toggle7.db')
        cursor = connection.cursor()
        load = 'light5'
        status = 'ON'
        if state == 'Load1on':
                sock.send('Load1ON')
                print ("sent: Load1ON")
                try:
                     cursor.execute("UPDATE user1 SET load = 'light5' WHERE id='1'")
                     connection.commit()
                     message = "success"
                except:
                     connection.rollback()
                     message = "failure"
                finally:
                     connection.close()
                     print (message)

Tags: 数据数据库appmessagesqliteloadconnectionroute
1条回答
网友
1楼 · 发布于 2024-10-03 13:23:25
UPDATE user1 SET load = 'light5' WHERE id='1'

此命令更新id列中值为'1'的所有行。 如果什么都没有发生,那么这就意味着没有这样的争吵。在

如果id列包含数字,则必须搜索一个数字:

^{pr2}$

而且您应该始终使用参数来避免格式问题(以及SQL注入攻击):

cursor.execute('UPDATE user1 SET load = ? WHERE id = ?', ['light5', 1])

相关问题 更多 >