简体   繁体   English

python sql转义正斜杠不起作用

[英]python sql escape forward slash not working

I am trying to execute an sql statement that should add some data to a database like so: 我正在尝试执行一条sql语句,该语句应将某些数据添加到数据库中,如下所示:

cursor.execute("INSERT INTO Actors (imdbPageId, fullName) VALUES (%s, %s)" % ( db.escape_string(self.imdbID), self.name))

I have also tried: 我也尝试过:

cursor.execute("INSERT INTO Actors (imdbPageId, fullName) VALUES (%s, %s)" % ( self.imdbID, self.name))

But i keep getting this error regardless of using the escape_string or not. 但是无论是否使用escape_string我都会不断收到此错误。 See below: 见下文:

MySQLdb._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 '/name/nm0991810, Mahershala Ali)' at line 1") MySQLdb._exceptions.ProgrammingError:(1064,“您的SQL语法有错误;请在第1行的'/ name / nm0991810,Mahershala Ali)附近,查看与MySQL服务器版本相对应的手册以使用正确的语法” )

I am pretty sure it has to do with the forward slash but i cant get it to work. 我很确定它与正斜杠有关,但我无法使其正常工作。 How do i fix this issue? 我该如何解决这个问题?

If any more information is needed let me know! 如果需要更多信息,请告诉我!

Do this instead: 改为这样做:

query = "INSERT INTO Actors (imdbPageId, fullName) VALUES (%s, %s)"

cursor.execute(query, (self.imdbID, self.name))

If I am not mistaken mysqldb takes care of this for you. 如果我没有记错的话, mysqldb会为您解决这个问题。

Otherwise you can do: 否则,您可以执行以下操作:

cursor.execute(query, (db.escape_string(self.imdbID), self.name))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM