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========================================================="
展开
 我来答
xpresslink
2015-01-20 · TA获得超过3705个赞
知道小有建树答主
回答量:272
采纳率:87%
帮助的人:100万
展开全部
写法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格式化字符串那部分吧,每本基础书前面都有。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式