我创建了一个数据库并使其工作到搜索特定的batchnr。 我想做的是如下:
输入batchnumber:1001
它会给我以下结果:
(1001, 90, 10001, 2, 500, 'Veghel', 100, 300)
然后问以下问题: “你想改变一些关于这一点的事情”
您要更改的值存储在“ChangeThis”中,您想要将其更改的值存储在“ToThis”中(必须将其翻译为英语,这就是为什么它非常基本)
例如,你会得到这样的东西:
(1001, 90, 10001, 2, 500, 'Veghel', 100, 300)
change what: 500
into: 9999
现在我想把这些变量放到我的python-SQL代码中,我试过这个:
conn = sqlite3.connect('DATABASE.db')
cur = conn.cursor()
ChangeThis = str(input("which value would you like to change?(value):"))
ToThis = str(input("In what would you like to change this?(value):"))
change = 'UPDATE batch SET ? WHERE ?' (ToThis, ChangeThis)
print(change)
cur.execute(change)
这给了我TypeError:'str'对象不可调用。
有没有人知道如何解决这个问题?
完整代码发布在下面:
import sqlite3
def create_connection():
try:
conn = sqlite3.connect('DATABASE.db')
return conn
except Error as e:
print(e)
def select_all():
conn = sqlite3.connect('DATABASE.db')
cur = conn.cursor()
batchnr = input("batchnumber:")
sqlquery = 'SELECT * FROM BATCH WHERE BATCHNR ='+ batchnr
#print(sqlquery)
cur.execute(sqlquery)
rows = cur.fetchall()
for row in rows:
print(row)
verander = input("Would you like to change something about this?Y/N: ")
if verander == 'Y' or verander == 'y':
veranderen()
else:
print("Shutdown")
def veranderen():
conn = sqlite3.connect('DATABASE.db')
cur = conn.cursor()
ChangeThis = str(input("which value would you like to change?(value):"))
ToThis = str(input("In what would you like to change this?(value):"))
change = 'UPDATE batch SET ? WHERE ?' (ToThis, ChangeThis)
print(change)
cur.execute(change)
create_connection()
select_all()
答案 0 :(得分:2)
问题在于这一行:
change = 'UPDATE batch SET ? WHERE ?' (ToThis, ChangeThis)
您正在定义一个字符串文字,然后尝试将其称为带有参数(ToThis, ChangeThis)
的函数。这永远不会起作用,因为例外情况说,'str'对象不可调用。参数不需要在该行上。将它们添加到cur.execute()
调用:
change = 'UPDATE batch SET ? WHERE ?'
cur.execute(change, (ToThis, ChangeThis))
......或者只是
cur.execute('UPDATE batch SET ? WHERE ?', (ToThis, ChangeThis))