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

发布于 2024-09-29 18:53:47 字数 698 浏览 1 评论 0原文

我正在尝试

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2024-10-06 18:53:47

错误语法:IF NOT EXISTS CREATE TABLE 在 MySQL 中不是有效的 SQL。

您需要

CREATE TABLE IF NOT EXISTS [tablename]

按照 MySQL 文档

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文