How can I create a TABLE if and only if it does not exist? [closed]
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开发者_JAVA技巧(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!
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.
精彩评论