CREATE TABLE IF NOT EXISTS 在 MySQLdb 中工作吗?句法?
我创建了一个表“table2”,并在运行代码时收到警告(表已存在)。 我只想在表不存在时创建它。 一些研究MySQL语法网站在MySQL中出现以下内容:CREATE TABLE IF NOT EXISTS
我的代码:
cursor.execute('CREATE TABLE IF NOT EXISTS (2 INT)`table2`')
提供此警告:
_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 '(2 INT)`table2`' at line 1")
我有数据库版本数据库版本:5.1.54-1ubuntu4 谢谢-汤姆
I've created a table "table2" and get warnings (table already exists) when I run my code.
I want to create the table only if it doesn't exist.
Some research MySQL syntax websiteturns up the following in MySQL : CREATE TABLE IF NOT EXISTS
My code:
cursor.execute('CREATE TABLE IF NOT EXISTS (2 INT)`table2`')
provides this warning:
_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 '(2 INT)`table2`' at line 1")
I have database version Database version : 5.1.54-1ubuntu4
Thanks-Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
mysql 语法
使用以下...
结果:
mysql syntax is
using the following...
result:
CREATE TABLE
命令的语法存在一些问题。在列定义之后有表名称。它应该放在他们面前,如下所示:
CREATE TABLE IF NOT EXISTS table2 ();
其次,您使用
2
作为列名称,但我不确定2
是否是有效的列名称。如果是,则应将其与普通整数区分开来。您可以在 MySQL 文档。
There are a couple of problems with the syntax of your
CREATE TABLE
command.You have the table name after the column definitions. It should be placed before them, like this:
CREATE TABLE IF NOT EXISTS table2 (<column definitions>);
Secondly, you are using
2
as a column name, but I'm not sure that2
is even a valid column name. If it is, it should be quoted to distinguish it from an plain integer.You can read more about the
CREATE TABLE
syntax at the MySQL documentation.首先,使用官方参考而不是一些随机站点。这些文档几乎肯定会好得多。其次,你的意思可能更接近于:
First, use the official reference and not some random site. The docs are almost certain to be a lot better. Second, you probably meant something closer to:
问题出在你的sql语法上。
CREATE TABLE
语句的正确语法是将表名放在列之前。此外,2 INT
不是有效的列定义。如果您的意思是该列的名称为“2”,则必须像这样引用它,或者如果(更有可能)您想要一个包含两位十进制数字的列,则长度放在数据类型之后;该列仍然必须有一个名称:
foo_column int(2)
所以总的来说,你想要
The problem is with your sql syntax. the correct syntax for a
CREATE TABLE
statement puts the table name before the columns. Furthermore,2 INT
is not a valid column definition. If you mean for the column to have the name "2" it must be quoted likeOr if (more likely) you want a column of two decimal digits, then the length is placed after the data type; the column must still be given a name:
foo_column int(2)
So altogether, you want