views:

72

answers:

1

I'm trying

conn = MySQLdb.connect (host = "localhost",
                           user = "username",
                           passwd = "password",
                           db = "my_db")
cursor = conn.cursor ()
q = """IF NOT EXISTS CREATE TABLE %s (
         course  VARCHAR(15),
         student  VARCHAR(15),
         teacher VARCHAR(15),
         timeslot VARCHAR(15))""" % (d,)

cursor.execute(q)

But I get the 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 'IF NOT EXISTS CREATE TABLE ACCOUNTG (\\n\\t course VARCHAR(15),\\n\\t s' at line 1")

I'm not sure what's wrong with what I'm trying, I just want to make a table if it doesn't exist. Any input would be appreciated, thanks!

+10  A: 

Wrong syntax: IF NOT EXISTS CREATE TABLE is not valid SQL in MySQL.

You want

CREATE TABLE IF NOT EXISTS [tablename]

per the MySQL documentation.

Jason S