0

I am working on a school project that pulls data from an excel sheet and puts the data in a database. After I run the code below and run the SQL command SELECT * FROM items, it returns "Empty set". What am I doing wrong? Thanks for the help in advance!

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 ---')

1 Answers1

1

You are missing the commit, you must commit your queries at the end of your INSERT's

db.commit()
lapinkoira
  • 8,320
  • 9
  • 51
  • 94