CREATE TABLE IF NOT EXISTS 在 MySQLdb 中工作吗?句法?

发布于 2024-12-27 23:32:46 字数 634 浏览 3 评论 0原文

我创建了一个表“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 技术交流群。

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

发布评论

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

评论(5

成熟的代价 2025-01-03 23:32:46

mysql 语法

CREATE TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]
    [partition_options]

使用以下...

cursor.execute('CREATE TABLE IF NOT EXISTS `table2` (`something` int(2))')

结果:

__main__:1: Warning: Table 'table2' already exists

mysql syntax is

CREATE TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]
    [partition_options]

using the following...

cursor.execute('CREATE TABLE IF NOT EXISTS `table2` (`something` int(2))')

result:

__main__:1: Warning: Table 'table2' already exists
巷雨优美回忆 2025-01-03 23:32:46

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

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

    CREATE TABLE IF NOT EXISTS table2 ();

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

您可以在 MySQL 文档

There are a couple of problems with the syntax of your CREATE TABLE command.

  1. 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>);

  2. Secondly, you are using 2 as a column name, but I'm not sure that 2 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.

三生一梦 2025-01-03 23:32:46

首先,使用官方参考而不是一些随机站点。这些文档几乎肯定会好得多。其次,你的意思可能更接近于:

CREATE TABLE IF NOT EXISTS table2 (columnname INT);

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:

CREATE TABLE IF NOT EXISTS table2 (columnname INT);
花期渐远 2025-01-03 23:32:46

问题出在你的sql语法上。 CREATE TABLE 语句的正确语法是将表名放在列之前。此外,2 INT 不是有效的列定义。如果您的意思是该列的名称为“2”,则必须像这样引用它

`2` INT

,或者如果(更有可能)您想要一个包含两位十进制数字的列,则长度放在数据类型之后;该列仍然必须有一个名称: foo_column int(2)

所以总的来说,你想要

cursor.execute('''
    CREATE TABLE IF NOT EXISTS table2 (
        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 like

`2` INT

Or 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

cursor.execute('''
    CREATE TABLE IF NOT EXISTS table2 (
        foo_column INT(2))
    ''')
雨巷深深 2025-01-03 23:32:46
import sqlite3
connexion = sqlite3.connect("bd-celebrites.sq3")
curseur = connexion.cursor()
curseur.execute("CREATE TABLE IF NOT EXISTS celebrites (nom TEXT, prenom TEXT, annee INTEGER)")
import sqlite3
connexion = sqlite3.connect("bd-celebrites.sq3")
curseur = connexion.cursor()
curseur.execute("CREATE TABLE IF NOT EXISTS celebrites (nom TEXT, prenom TEXT, annee INTEGER)")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文