I've been writing code recently in python that works with postgreSQL databases, and it looks like the following:
def insert(table, ID, date, timestamp):
cur.execute("""INSERT INTO %s (ID, date, timest)
VALUES (%s, %s, %s);""",
(AsIs(table), ID, AsIs(date), AsIs(timestamp)))
This seems to work fine for postgreSQL, but when I try the same thing on a mySQL server, it doesn't work. Does anybody have an idea as to why? I think it might have to do with the %s characters, as it works if I hardcode in the values for table, ID, date, and timestamp.
In addition, it gives me the following error:
_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 ''master'' at line 2")
'master' is the name of the table I used as a variable, and I also noticed that there were two single quotes around master (like so: ' 'master' '), where there should only be one. So this might be a problem too?
Anyways, thanks for the help!
execute
command? – TheSoundDefense Jul 24 '14 at 23:53query_str = "INSERT INTO %s (ID, date, timest) VALUES (%s, %s, %s);" % (str(table), str(ID), str(date), str(timestamp))
followed bycur.execute(query_str)
. I dunno if this will work, I haven't used Python's SQL libraries. – TheSoundDefense Jul 24 '14 at 23:57AsIS
is apsycopg2
adapter. What driver are you using for MySQL? – Clodoaldo Neto Jul 25 '14 at 0:01