CREATE TABLE IF NOT EXISTS 在 MySQLdb 中是否有效?句法? [英] Does CREATE TABLE IF NOT EXISTS work in MySQLdb? Syntax?

查看:54
本文介绍了CREATE TABLE IF NOT EXISTS 在 MySQLdb 中是否有效?句法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表table2"并在运行代码时收到警告(表已存在).我只想在表不存在时创建它.一些研究 MySQL 语法网站在 MySQL 中出现以下内容:CREATE TABLE IF NOT EXISTS

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

我的代码:

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 have database version Database version : 5.1.54-1ubuntu4 Thanks-Tom

推荐答案

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

这篇关于CREATE TABLE IF NOT EXISTS 在 MySQLdb 中是否有效?句法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆