2010-10-29 46 views
1

我想如何創建TABLE當且僅當它不存在?

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) 

但我得到的錯誤:_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")

我不知道這有什麼錯什麼,我想,我只想做一個表,如果它不存在。任何意見,將不勝感激,謝謝!

回答

13

語法錯誤:IF NOT EXISTS CREATE TABLE在MySQL中無效。

你想

CREATE TABLE IF NOT EXISTS [tablename] 

MySQL documentation

相關問題