更新语句不影响sqlite表

时间:2019-05-23 15:12:50

标签: python sqlite

我是sqlite的新手。

我使用python创建了一个带有营养素表的sqlite数据库(请参阅屏幕快照中的结构)。

Screen shot of nutrients table

我已经使用

填充了表格
INSERT INTO nutrients ...

但是,我似乎无法更新一行。以下代码

import sqlite3

path = 'nutrition.sqlite'

command = 'SELECT user_name FROM nutrients WHERE code="ALCO"'
connection = sqlite3.connect(path)
cursor = connection.cursor()
cursor.execute(command)
rows = cursor.fetchall()
cursor.close()
connection.close()
print('before update', rows[0])

command = 'UPDATE nutrients SET user_name="Booze" WHERE code="ALCO"'
connection = sqlite3.connect(path)
cursor = connection.cursor()
cursor.execute(command)
cursor.close()
connection.close()

command = 'SELECT user_name FROM nutrients WHERE code="ALCO"'
connection = sqlite3.connect(path)
cursor = connection.cursor()
cursor.execute(command)
rows = cursor.fetchall()
cursor.close()
connection.close()
print('after update', rows[0])

产生输出

('before update', (u'Alcohol',))
('after update', (u'Alcohol',))

0 个答案:

没有答案