Python 如何从表中删除记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3977570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a record from table?
提问by Risino
I have a problem with deleting a record from my SQLite3 database:
从我的 SQLite3 数据库中删除记录时遇到问题:
conn = sqlite3.connect('databaza.db')
c = conn.cursor()
data3 = str(input('Please enter name: '))
mydata = c.execute('DELETE FROM Zoznam WHERE Name=?', (data3,))
conn.commit()
c.close
Everything is OK, no errors, but the delete function doesn't work!
一切正常,没有错误,但是删除功能不起作用!
Does anyone have an idea?
有没有人有想法?
采纳答案by Risino
Thank you to everybody who tried to help. The right code is:
感谢所有试图提供帮助的人。正确的代码是:
conn = sqlite3.connect('databaza.db')
c = conn.cursor()
conn.text_factory = str
data3 = str(input('Please enter name: '))
query = "DELETE FROM Zoznam WHERE Name = '%s';" % data3.strip()
print(query)
mydata = c.execute(query)
回答by Andrew Sledge
Check the file permissions.
检查文件权限。
An aside, I prefer the tokenized method:
顺便说一句,我更喜欢标记化方法:
mydata = c.execute("DELETE FROM Zoznam WHERE Name='%s'" % data3)
回答by Kelmer
I advise you to first make a string for the query , and then execute it. ex:
我建议您先为查询创建一个字符串,然后执行它。前任:
query = "delete from zoznam where name = '%s' " % data3
c.execute(query)
conn.commit()
回答by Cameron Gagnon
The correct syntax for a parameterizedquery is:
参数化查询的正确语法是:
mydata = c.execute("DELETE FROM Zoznam WHERE Name=?", (data3,))
Make sure the parameter uses the comma, to make it a python tuple.
确保参数使用逗号,使其成为 python 元组。
This will help prevent SQL Injection which is possible when passing in a formatted string. More on SQL Injection here
这将有助于防止在传入格式化字符串时可能发生的 SQL 注入。更多关于 SQL 注入在这里
Related post here.
相关帖子在这里。
回答by Francisco de Larra?aga
Try with:
尝试:
mydata = c.execute('DELETE FROM Zoznam WHERE Name = (?)', (data3))
Without the ',' and the '?' between '()'
没有 ',' 和 '?' 之间 '()'
回答by Dregnox
I'm a little late to the party but if you Google search "python sqlite delete row" This is the first thing that comes up and I was having the same issue where things were not getting DELETE'd from my sqlite DB. I'm using Python 2.7 on Debian Jessie.
我参加聚会有点晚了,但是如果您在 Google 上搜索“python sqlite 删除行”,这是第一个出现的问题,我遇到了同样的问题,我的 sqlite 数据库中的内容没有被删除。我在 Debian Jessie 上使用 Python 2.7。
Previously, when I wrote Python code for adding and retrieving information in the sqlite database, I had written the commands with correct capitalization where needed and it worked.
以前,当我编写用于在 sqlite 数据库中添加和检索信息的 Python 代码时,我已经在需要的地方使用正确的大小写编写了命令并且它起作用了。
curs.execute("SELECT example_column1 FROM example_table WHERE example_column2=(?)", (Variable,))
However...
然而...
curs.execute("DELETE FROM example_table WHERE example_column1=(?)", (Variable,)):
This for some reason does not work with the DELETE command. I had to send that command in all lower-case before sqlite would respect the command being sent.
由于某种原因,这不适用于 DELETE 命令。在sqlite尊重发送的命令之前,我必须以全部小写形式发送该命令。
conn=sqlite3.connect('example.db')
curs=conn.cursor()
curs.execute("delete from example_table where example_column=(?)", (Variable,))
conn.commit()
conn.close()
I have no idea why. I tried all the previously mentioned methods but having the command sent in lowercase was the only way I could get it to work. Hope this helps any struggling neophytes in their journey into Python and sqlite.
我不知道为什么。我尝试了所有前面提到的方法,但以小写形式发送命令是我让它工作的唯一方法。希望这有助于任何在 Python 和 sqlite 之旅中苦苦挣扎的新手。

