我正在尝试编写将连接到xampp中mysql数据库的代码。到目前为止,我已经能够成功连接并创建表,但是当我尝试将数据上传到表中时,会不断出现此错误:
您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的''附近使用
代码复制到这里:
!/usr/bin/python3
import pymysql
# open file
file = open("UCI_Credit_Card.csv", "r")
readfile = file.read()
# convert string to l
l = []
for line in readfile.split("/n"):
l.append(line.split(","))
# Open database connection
db = pymysql.connect("localhost", "testuser", "test123", "info_153_lab6")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
#cursor.execute("DROP TABLE IF EXISTS UCI_Credit_Card")
# create column names
#ID = l[0][0];
#LIMIT_BAL = l[0][1];
#SEX = l[0][2];
#EDUCATION = l[0][3];
#MARRIAGE = l[0][4];
#AGE = l[0][5];
#PAY_0 = l[0][6];
#PAY_2 = l[0][7];
#PAY_3 = l[0][8];
#PAY_4 = l[0][9];
#PAY_5 = l[0][10];
#PAY_6 = l[0][11];
#BILL_AMT1 = l[0][12];
#BILL_AMT2 = l[0][13];
#BILL_AMT3 = l[0][14];
#BILL_AMT4 = l[0][15];
#BILL_AMT5 = l[0][16];
#BILL_AMT6 = l[0][17];
#PAY_AMT1 = l[0][18];
#PAY_AMT2 = l[0][19];
#PAY_AMT3 = l[0][20];
#PAY_AMT4 = l[0][21];
#PAY_AMT5 = l[0][22];
#PAY_AMT6 = l[0][23];
#DPNM = l[0][24]
# Create table as per requirement
#sql = """CREATE TABLE UCI_Credit_Card (
# ID INT NOT NULL,
# LIMIT_BAL FLOAT,
# SEX CHAR(1),
# EDUCATION CHAR(1),
# MARRIAGE CHAR(1),
# AGE INT,
# PAY_0 INT,
# PAY_2 INT,
# PAY_3 INT,
# PAY_4 INT,
# PAY_5 INT,
# PAY_6 INT,
# BILL_AMT1 FLOAT,
# BILL_AMT2 FLOAT,
# BILL_AMT3 FLOAT,
# BILL_AMT4 FLOAT,
# BILL_AMT5 FLOAT,
# BILL_AMT6 FLOAT,
# PAY_AMT1 FLOAT,
# PAY_AMT2 FLOAT,
# PAY_AMT3 FLOAT,
# PAY_AMT4 FLOAT,
# PAY_AMT5 FLOAT,
# PAY_AMT6 FLOAT,
# DPNM CHAR(1))"""
#cursor.execute(sql)
del l[0]
# generate multiple values from the l to be placed in the query
rows = " "
for i in range(len(l) - 1):
rows += "('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', " \
"'{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}')".format(l[i][0], l[i][1], l[i][2], l[i][3], l[i][4],
l[i][5], l[i][6], l[i][7], l[i][8], l[i][9], l[i][10], l[i][11], l[i][12], l[i][13], l[i][14], l[i][15],
l[i][16], l[i][17], l[i][18], l[i][19], l[i][20], l[i][21], l[i][22], l[i][23], l[i][24])
if i != len(l) - 2:
rows += ','
# add values into the table
queryInsert = "INSERT INTO UCI_Credit_Card VALUES" + rows
cursor.execute(queryInsert)
db.commit()
# disconnect from server
db.close()