[英]Inserting CSV data into MySQL database - "You have an error in your SQL syntax; near ' ' at line 1
This is the exact error message I am getting: 这是我得到的确切错误消息:
(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 '' at line 1")
And here is my code. 这是我的代码。
def mysql_connect():
conn = MySQLdb.connect(host = "localhost",
user = "root",
passwd = "********",
db = "seomonster")
cursor = conn.cursor()
filename = 'tdnam_all_listings.csv'
filename2 = 'tdnam_all_listings2.csv'
filename3 = 'tdnam_all_listings3.csv'
try:
table_create = """CREATE TABLE sites (domainName CHAR(40),
itemID INT,auctionType CHAR(40),
timeLeft CHAR(40),
price CHAR(20),
bids INT,
domainAge INT,
traffic INT,
valuationPrice CHAR(40))"""
cursor.execute(table_create)
except Exception as e:
print e
try:
csv_data = csv.reader(file(filename))
for row in csv_data:
cursor.execute("""INSERT INTO sites(domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice)""")
except Exception as e2:
print e2
conn.commit()
cursor.close()
conn.close()
I do not understand the error at all, although I assume it has something to do with the SQL syntax I am using by inserting the data? 我完全不理解该错误,尽管我认为它与我通过插入数据使用的SQL语法有关?
@bernie Changed my code to this: @bernie将我的代码更改为此:
cursor.execute("""INSERT INTO sites(domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice) values (%s%s,%s,%s,%s,%s,%s,%s,%s);""")
Now getting this error: 现在出现此错误:
(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 '%s%s,%s,%s,%s,%s,%s,%s,%s)' at line 1")
@VMai I followed your advice and changed my code to this: @VMai我按照您的建议,将代码更改为:
for row in csv_data:
insert = """INSERT INTO sites (domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice) values (%s,%s,%s,%s,%s,%s,%s,%s,%s);"""
cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
I am getting a long error which I assume has something to do with the %s's some of the columns's are integers that is why I think. 我遇到了一个很长的错误,我认为这与%s的某些列有关都是整数,这就是我认为的原因。 Error: 错误:
Warning (from warnings module):
File "/var/www/seomonster/ZeroPain.py", line 109
cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' ItemID' for column 'itemID' at row 1
Warning (from warnings module):
File "/var/www/seomonster/ZeroPain.py", line 109
cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' Bids' for column 'bids' at row 1
Warning (from warnings module):
File "/var/www/seomonster/ZeroPain.py", line 109
cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' Domain Age' for column 'domainAge' at row 1
Warning (from warnings module):
File "/var/www/seomonster/ZeroPain.py", line 109
cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' Traffic' for column 'traffic' at row 1
Warning (from warnings module):
File "/var/www/seomonster/ZeroPain.py", line 109
cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Data truncated for column 'domainName' at row 1
I suppose your only missing one comma after your first query parameter %s
as you have 9 fields to fill but only 8 parameters in the end. 我想您在第一个查询参数%s
之后只缺少一个逗号,因为您有9个字段要填写,但最后只有8个参数。 First and second %s
will be concatenated because of the missing comma between them. 由于第一和第二个%s
之间缺少逗号,因此它们将被连接起来。
Second, as already stated in the comments, pass your arguments as second parameter to this function. 第二,如注释中所述,将您的参数作为第二个参数传递给此函数。
Try this: 尝试这个:
cursor.execute("""INSERT INTO sites (domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice) values (%s,%s,%s,%s,%s,%s,%s,%s,%s);""", (row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.