使用python scrip创建SQL表时出错

2024-04-18 13:15:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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")

Tags: inexecutesqluseservicelinephonecode
1条回答
网友
1楼 · 发布于 2024-04-18 13:15:05

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

SQL manual-

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

相关问题 更多 >