【发布时间】:2020-03-20 10:06:17
【问题描述】:
day = datetime.datetime.today().strftime('%Y%m%d')
def connect():
conn = None
try:
conn = mysql.connector.connect(user='elijah', password='12345678',
host='127.0.0.1', database='userdb',
auth_plugin='mysql_native_password')
if conn.is_connected():
print('Connected to MySQL database')
cursor = conn.cursor()
sql = '''CREATE TABLE %s(
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
rating VARCHAR(255) NOT NULL,
reservation_rate VARCHAR(255) NOT NULL
)'''%day
cursor.execute(sql)
我试图在表名中插入一个变量,但它一直给我这个错误 :
1064 (42000): 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 '20200320(
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) ' at line 1
我已经尝试过格式、字符串连接和 %s 替换,到目前为止没有任何效果。
【问题讨论】:
标签: python mysql sql variables syntax-error