python sql转义正斜杠不工作

2024-09-29 17:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试执行一个sql语句,该语句应该向数据库添加一些数据,如下所示:

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

我也尝试过:

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

但是不管是否使用escape_string,我总是会得到这个错误。见下表:

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")

我很肯定这和正斜杠有关,但我不能让它起作用。如何解决此问题?你知道吗

如果需要更多信息,请告诉我!你知道吗


Tags: nameselfexecuteyourstringactors语句cursor
1条回答
网友
1楼 · 发布于 2024-09-29 17:22:09

请改为:

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

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

如果我没弄错的话mysqldb会帮你处理好的。你知道吗

否则您可以:

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

相关问题 更多 >

    热门问题