gpt4 book ai didi

python - 使用python脚本创建SQL表出错

转载 作者:行者123 更新时间:2023-11-29 19:26:13 24 4
gpt4 key购买 nike

我正在尝试使用 python 脚本创建一个 sql 表。

这是代码:

import MySQLdb
db1 = MySQLdb.connect(host="localhost",user="root",passwd="")
cursor = db1.cursor()
sql = 'use test'
cursor.execute(sql)
query='CREATE TABLE IF NOT EXISTS 3112017(service_area_code VARCHAR(100),phone_numbers VARCHAR(100),preferences VARCHAR(100),opstype VARCHAR(100),phone_type VARCHAR(100))'
cursor.execute(query)
db1.commit()

以下是我收到的错误:

cursor.execute(query)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
_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 '3112017(service_area_code VARCHAR(100),phone_numbers VARCHAR(100),preferences VA' at line 1")

最佳答案

正如回溯中提到的,您的 SQL 语法有错误。表名错误。

来自SQL manual -

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

关于python - 使用python脚本创建SQL表出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com