我想定义一个函数(名为“ kayitEkle”),该函数将函数的参数插入数据库中的表(名为“ biTablo”):
import sqlite3
connect = sqlite3.connect("obs.db")
cursor = connect.cursor()
def tabloOlustur():
cursor.execute("CREATE TABLE IF NOT EXISTS biTablo(ad TEXT, soyad TEXT, numara TEXT, puan REAL)")
connect.commit()
tabloOlustur()
def kayitEkle(ad, soyad, numara, puan):
cursor.execute("INSERT INTO biTablo(ad, soyad, numara, puan) VALUES(? ? ? ?)",(ad,soyad,numara,puan))
connect.commit()
kayitEkle('ahmet', 'yılmaz', '08067', 50)
但我收到此消息:
Traceback (most recent call last):
File "C:/Users/pc/PycharmProjects/ikinciBahar/ogrenmeDatabase.py", line 234, in <module>
kayitEkle('ahmet', 'yılmaz', '08067', 50)
File "C:/Users/pc/PycharmProjects/ikinciBahar/ogrenmeDatabase.py", line 231, in kayitEkle
cursor.execute("INSERT INTO biTablo(ad, soyad, numara, puan) VALUES(? ? ? ?)",(ad,soyad,numara,puan))
sqlite3.OperationalError: near "?": syntax error
怎么了?我该怎么办?
答案 0 :(得分:1)
在您的cursor.execute
中使用VALUES(%s, %s, %s, %s)
答案 1 :(得分:1)
您需要正确组成字符串。假设广告,soyad,numara是字符串,而puan是数字:
cursor.execute(INSERT INTO biTablo VALUES(\“%s \”,\“%s \”,\“%s \”,%f);“%(ad,soyad,numara,puan))< / p>
答案 2 :(得分:1)
您可能应该使用python .format
并将其行更改为
cursor.execute("INSERT INTO biTablo(ad, soyad, numara, puan) VALUES(? ? ? ?)",(ad,soyad,numara,puan))
到
cursor.execute("INSERT INTO biTablo(ad, soyad, numara, puan) VALUES({},{},{},{})".format(ad,soyad,numara,puan))
答案 3 :(得分:0)
如果我没记错,这应该可以工作:
cursor.execute("INSERT INTO biTablo(ad, soyad, numara, puan) VALUES(%s, %s, %s, %s)", (ad,soyad,numara,puan))