当且仅当 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,“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以获取在 'IF NOT 附近使用的正确语法” EXISTS CREATE TABLE ACCOUNTG (\\n\\t course VARCHAR(15),\\n\\t s' at line 1")
我不确定我正在尝试的有什么问题,我只是想如果不存在,请制作一个表格。任何输入将不胜感激,谢谢!
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(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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误语法:
IF NOT EXISTS CREATE TABLE
在 MySQL 中不是有效的 SQL。您需要
按照 MySQL 文档。
Wrong syntax:
IF NOT EXISTS CREATE TABLE
is not valid SQL in MySQL.You want
per the MySQL documentation.