2017-06-22 61 views
0

我正在從一個學校項目工作,從Excel表單中提取數據並將數據放入數據庫中。在運行下面的代碼並運行SQL命令SELECT * FROM items後,它將返回「空集」。我究竟做錯了什麼?我在這裏先向您的幫助表示感謝!Python/MySQL:表沒有得到更新

import openpyxl 
import MySQLdb as mdb 

db = mdb.connect('localhost', 'root', 'TRM4611', 'practice') 
cur = db.cursor() 

wb_choice = input('What workbook would you like to use?\n--> ') 

wb = openpyxl.load_workbook(wb_choice + '.xlsx') 

all_sheets = wb.get_sheet_names() 

with db: 
    for sheet in all_sheets: 
     current_sheet = wb.get_sheet_by_name(sheet) 
     print ('\nCurrent Sheet: ' + current_sheet.title) 
     for i in range(current_sheet.max_column): 
      for cellObj in current_sheet.columns[i]: 
       if i == 0: 
        cur.execute("INSERT INTO items(Date) VALUES(%s)", (cellObj.value,)) 
       if i == 1: 
        cur.execute("INSERT INTO items(Fruit) VALUES(%s)", (cellObj.value,)) 
       if i == 2: 
        cur.execute("INSERT INTO items(Quantity) VALUES(%s)", (cellObj.value,)) 

       print (cellObj.coordinate, cellObj.value) 
      print ('--- END OF ROW ---') 
+0

嘗試db.commit年底提交你的查詢()後cur.execute() –

回答

1

你缺少的承諾,您必須在INSERT的

db.commit()