Python中操作sqlite3 关于占位符的问题
求助:用占位符的方式实现Select、Update、Delete,请赐教!(Insert用占位符的方式是OK的)删除的写法:现状(写法1OK,写法2NG,写法3NG)写法...
求助:用占位符的方式实现Select、Update、Delete,请赐教!(Insert用占位符的方式是OK的)
删除的写法:现状(写法1OK,写法2NG,写法3NG)
写法1:
print "======Delete sample========================================================="
cur.execute("delete from pm_group where groupname = '天神' or groupname = '中国语'")
conn.commit()
print "Delete ",cur.rowcount,"Record"
print "======Delete sample========================================================="
写法2:cur.execute("delete from pm_group where groupname=?", [("天神"),("中国语")])
Error内容:
Traceback (most recent call last):
File "D:\PASSManager\passmanager.py", line 46, in <module>
cur.execute("delete from pm_group where groupname=?", [("天神"),("中国语")])
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.
写法3:
print "======Delete sample========================================================="
d = "天神"
cur.execute("delete from pm_group where groupname=?", d)
conn.commit()
print "Delete ",cur.rowcount,"Record"
print "======Delete sample========================================================="
Error内容:
Traceback (most recent call last):
File "D:\PASSManager\passmanager.py", line 48, in <module>
cur.execute("delete from pm_group where groupname=?", d)
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 6 supplied.
----------------------------------------------分割---------------------------------------------------------------------------
Insert OK的例子:
print "======Insert sample========================================================="
for t in [(4,'日本语'),(5, '天神')]:
cur.execute("insert into pm_group(id,groupname) values (?,?)", t)
conn.commit()
print "Insert done successfully"
print "======Insert sample=========================================================" 展开
删除的写法:现状(写法1OK,写法2NG,写法3NG)
写法1:
print "======Delete sample========================================================="
cur.execute("delete from pm_group where groupname = '天神' or groupname = '中国语'")
conn.commit()
print "Delete ",cur.rowcount,"Record"
print "======Delete sample========================================================="
写法2:cur.execute("delete from pm_group where groupname=?", [("天神"),("中国语")])
Error内容:
Traceback (most recent call last):
File "D:\PASSManager\passmanager.py", line 46, in <module>
cur.execute("delete from pm_group where groupname=?", [("天神"),("中国语")])
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied.
写法3:
print "======Delete sample========================================================="
d = "天神"
cur.execute("delete from pm_group where groupname=?", d)
conn.commit()
print "Delete ",cur.rowcount,"Record"
print "======Delete sample========================================================="
Error内容:
Traceback (most recent call last):
File "D:\PASSManager\passmanager.py", line 48, in <module>
cur.execute("delete from pm_group where groupname=?", d)
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 6 supplied.
----------------------------------------------分割---------------------------------------------------------------------------
Insert OK的例子:
print "======Insert sample========================================================="
for t in [(4,'日本语'),(5, '天神')]:
cur.execute("insert into pm_group(id,groupname) values (?,?)", t)
conn.commit()
print "Insert done successfully"
print "======Insert sample=========================================================" 展开
1个回答
展开全部
写法2:cur.execute("delete from pm_group where groupname=?", [("天神"),("中国语")])
这个改成cur.execute("delete from pm_group where groupname=%s or groupname=%s " % ("天神","中国语"))
写法3:cur.execute("delete from pm_group where groupname=?", d)
这个改成 cur.execute("delete from pm_group where groupname=%s" % d)
你去补习一下python格式化字符串那部分吧,每本基础书前面都有。
这个改成cur.execute("delete from pm_group where groupname=%s or groupname=%s " % ("天神","中国语"))
写法3:cur.execute("delete from pm_group where groupname=?", d)
这个改成 cur.execute("delete from pm_group where groupname=%s" % d)
你去补习一下python格式化字符串那部分吧,每本基础书前面都有。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询