开发者

How can I create a TABLE if and only if it does not exist? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜