簡體   English   中英

使用python將excel數據導入mysql時出錯,僅插入一行

[英]getting error while importing excel data into mysql using python only one row inserted

我試圖在視頻教程的幫助下使用 Python 將 excel 數據導入 MySQL,但我遇到以下問題:

僅將excel文件中的第一行插入到mysql中並出現以下錯誤

cursor.execute(query, values) line 179 in execute

這是我的代碼

import xlrd
import MySQLdb
book=xlrd.open_workbook("C:\Python27\mygdata.xls")
sheet=book.sheet_by_name("Sheet1")

database=MySQLdb.connect(host="Localhost", user="root", passwd="sharan246", db="test1")
cursor=database.cursor()
query=""" INSERT INTO omrdata (regno,name,subject,barcode,flag1) values (%s,%s,%s,%s,%s)"""
for r in range(1, sheet.nrows):
    regno = sheet.cell(r, 0).value
    name= sheet.cell(r, 1).value
    subject=sheet.cell(r, 2).value
    barcode=sheet.cell(r, 3).value
    flag1=sheet.cell(r, 4).value
    values=(regno,name,subject,barcode,flag1)
    cursor.execute(query, values)
    cursor.close()
    database.commit()
    database.close()
    print ""
    print "All done bye for now"
    print ""
    columns=str(sheet.ncols)
    print"i Just Imported"

您在 for 循環內關閉游標cursor.close()以及database.close() ,但在開始時只打開一次。 嘗試將這些行移到 for 循環之外,看看它是否會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM