FutureCake
FutureCake

Reputation: 2943

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:

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

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!

Upvotes: 0

Views: 701

Answers (1)

gold_cy
gold_cy

Reputation: 14216

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.

Otherwise you can do:

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

Upvotes: 1

Related Questions