![](/img/trans.png)
[英]“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”
[英]Inserting CSV data into MySQL database - "You have an error in your SQL syntax; near ' ' at line 1
這是我得到的確切錯誤消息:
(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")
這是我的代碼。
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()
我完全不理解該錯誤,盡管我認為它與我通過插入數據使用的SQL語法有關?
@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);""")
現在出現此錯誤:
(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我按照您的建議,將代碼更改為:
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]))
我遇到了一個很長的錯誤,我認為這與%s的某些列有關都是整數,這就是我認為的原因。 錯誤:
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
我想您在第一個查詢參數%s
之后只缺少一個逗號,因為您有9個字段要填寫,但最后只有8個參數。 由於第一和第二個%s
之間缺少逗號,因此它們將被連接起來。
第二,如注釋中所述,將您的參數作為第二個參數傳遞給此函數。
嘗試這個:
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.