如何更新SQL数据库中的值?SQLite/Python
我已经创建了一个表,并在表中插入了数据。我想知道如何更新/编辑数据。例如,如果表中有多个列,其中一列名为“age”,该列的数据为=“17”,而我现在想用“18”替换“17”,我会执行以下操作吗如何更新SQL数据库中的值?SQLite/Python,python,sql,sqlite,Python,Sql,Sqlite,我已经创建了一个表,并在表中插入了数据。我想知道如何更新/编辑数据。例如,如果表中有多个列,其中一列名为“age”,该列的数据为=“17”,而我现在想用“18”替换“17”,我会执行以下操作吗 import sqlite3 as lite import sys con = lite.connect('Records.db') with con: cur = con.cursor() cur.execute("INSERT INTO ExampleTable(Age)
import sqlite3 as lite
import sys
con = lite.connect('Records.db')
with con:
cur = con.cursor()
cur.execute("INSERT INTO ExampleTable(Age) VALUES(18) WHERE (Age = 17)")
我不喜欢Python,但我想我能帮上忙,所以
cur.execute("UPDATE ExampleTable SET age = 18 WHERE age = 17")
如果我错了,很抱歉,那么要使用Python中的SQLite库更新SQL数据库中的值,请使用如下语句
cur.execute(“更新示例集Age=18,其中Age=17”)
有关在Python中使用SQLite的详细介绍,请参阅。在sqlite3中与Python3.x一起使用,如下所示:
newPrice = '$19.99'
book_id = 4
cursor.execute('''UPDATE books SET price = ? WHERE id = ?''', (newPrice, book_id))
必须使用更新操作而不是插入操作来更改记录
计划如下
cur=con.cursor()
com="update ExampleTable set age=1 where age=17"
try:
cur.execute(com)
con.commit()
except:
print('error')
con.rollback()
con.close()
您将执行一个更新查询,这应该包含在学习SQL所用的任何教程中。
cur=con.cursor()
com="update ExampleTable set age=1 where age=17"
try:
cur.execute(com)
con.commit()
except:
print('error')
con.rollback()
con.close()