python mysqldb删除行

python mysqldb删除行,python,mysql,Python,Mysql,出于某种原因,这就是轰炸: print tbl,record statmt="DELETE FROM '%s' WHERE email LIKE '%s'" %(tbl,record) print statmt self.cursor.execute(statmt) 错误: maillist_frogs test@testovich.com DELETE FROM 'maillist_frogs' WHERE email LIKE 'test@testovich.com' Traceback

出于某种原因,这就是轰炸:

print tbl,record
statmt="DELETE FROM '%s' WHERE email LIKE '%s'" %(tbl,record)
print statmt
self.cursor.execute(statmt)
错误:

maillist_frogs test@testovich.com
DELETE FROM 'maillist_frogs' WHERE email LIKE 'test@testovich.com'
Traceback (most recent call last):
  File "./compare.py", line 123, in <module>
    main()
  File "./compare.py", line 117, in main
    remove_mailgust = sql_mailgust.removeRow ("maillist_frogs",x)
  File "./compare.py", line 81, in removeRow
    self.cursor.execute(statmt)
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/cursors.py", line 174, in execute
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''maillist_frogs' WHERE email LIKE 'test@testovich.com'' at line 1")
邮件列表\u青蛙test@testovich.com
从“邮件列表\青蛙”中删除,其中的电子邮件类似于test@testovich.com'
回溯(最近一次呼叫最后一次):
文件“/compare.py”,第123行,在
main()
文件“/compare.py”,第117行,主
remove\u mailgust=sql\u mailgust.removeow(“邮件列表\u青蛙”,x)
文件“/compare.py”,第81行,在removeRow中
self.cursor.execute(statmt)
文件“build/bdist.macosx-10.6-intel/egg/MySQLdb/cursors.py”,执行中第174行
文件“build/bdist.macosx-10.6-intel/egg/MySQLdb/connections.py”,第36行,在defaulterrorhandler中
_mysql_exceptions.ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的mysql服务器版本对应的手册,以了解在类似电子邮件的“邮件列表”附近使用的正确语法”test@testovich.com“第1行”)
谢谢!!:)

您有两个问题:

导致崩溃的第一个原因是使用常规引号引用了表名。如果要引用表名,应使用反引号(`)。

第二个问题,你“做得不对”。您不应该使用字符串格式创建查询,而是让Python MySQLdb的
游标正确地执行

要执行此操作,请尝试以下操作:

statmt = "DELETE FROM %s WHERE email LIKE %s" 
self.cursor.execute(statmt, (tbl, record))

顺便说一句,在MySQLdb查询中使用字符串格式会使您暴露在SQL注入中,以防您要在Web或其他地方使用应用程序。您不应该使用字符串格式来创建查询。

我刚才遇到了类似的问题

经过进一步的研究,我意识到我忘记了在删除之后执行
连接.commit()
,或者,正如我在其他地方发现的那样,你可以在执行任何其他数据操作之前执行
游标.execute(“set autocommit=1”)
,让它们立即自动提交,如果您不需要控制事务,请提交


这可能是Cmag代码的问题所在,虽然看不到更多,但很难说清楚。

这是怎么回事:
statmt=“从%s中删除,其中像“%s”这样的电子邮件(tbl,记录)
。如果我替换self.cursor.execute(“从%s.email中删除,其中像“%s”这样的电子邮件,tbl,记录),那么我会得到TypeError:execute()最多包含3个参数(给出4个参数)修复:,现在的问题是脚本只是循环,没有sql记录被删除,但没有异常被引发?
cursor.execute
调用应该返回删除的行数,您可以打印并检查它吗?你能再次检查数据库中是否存在记录吗?脚本首先比较了数据库,所以是的,sureWell的记录存在。我尝试使用你的代码,它对我有效,所以你的问题可能在数据库中的某个地方,你仔细检查了吗?你有必要的权限吗?也许您可以尝试使用MySQL的CLI来实现它?您确定要删除,并且x[0]是您所期望的吗?似乎很奇怪,
cursor.execute
调用将返回None,因为如果没有删除任何行,它将返回0,那里一切正常吗?
statmt = "DELETE FROM %s WHERE email LIKE %s" 
self.cursor.execute(statmt, (tbl, record))