代码之家  ›  专栏  ›  技术社区  ›  Parker

如果表不存在,我如何才能创建它?[关闭]

  •  1
  • Parker  · 技术社区  · 14 年前

    我正在努力

    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")

    我不知道我正在尝试的是什么问题,我只是想做一个桌子,如果它不存在。如有任何意见,我们将不胜感激,谢谢!

    1 回复  |  直到 14 年前
        1
  •  14
  •   Jason S    14 年前

    IF NOT EXISTS CREATE TABLE

    CREATE TABLE IF NOT EXISTS [tablename]
    

    MySQL documentation