当且仅当表不存在时,如何创建表?

2024-10-11 16:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我在努力

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

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


Tags: thetodbyourifcreateexiststable

热门问题