如果不存在,则创建表是否可以在MySQLdb中工作?句法?

Tom*_*Tom 4 python mysql-python

我创建了一个表“ table2”,并在运行代码时收到警告(表已存在)。我只想创建不存在的表。一些研究MySQL语法的网站在MySQL中提供了以下内容:如果不存在则创建表

我的代码:

cursor.execute('CREATE TABLE IF NOT EXISTS (2 INT)`table2`')
Run Code Online (Sandbox Code Playgroud)

提供以下警告:

_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")
Run Code Online (Sandbox Code Playgroud)

我有数据库版本数据库版本:5.1.54-1ubuntu4汤姆

use*_*149 5

mysql语法是

CREATE TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]
    [partition_options]
Run Code Online (Sandbox Code Playgroud)

使用以下...

cursor.execute('CREATE TABLE IF NOT EXISTS `table2` (`something` int(2))')
Run Code Online (Sandbox Code Playgroud)

结果:

__main__:1: Warning: Table 'table2' already exists
Run Code Online (Sandbox Code Playgroud)

  • 先执行“导入警告”,然后执行“ warnings.filterwarnings('ignore')” (5认同)

srg*_*erg 4

您的命令语法存在一些问题CREATE TABLE

  1. 列定义后面有表名称。它应该放在他们面前,如下所示:

    CREATE TABLE IF NOT EXISTS table2 (<column definitions>);

  2. 其次,您使用2作为列名称,但我不确定这2是否是有效的列名称。如果是,则应将其加引号以将其与普通整数区分开。

CREATE TABLE您可以在MySQL 文档中阅读有关语法的更多信息。