【问题标题】:Python/MySQL: Table isn't getting updated [duplicate]Python/MySQL:表没有得到更新[重复]
【发布时间】:2017-11-25 22:22:15
【问题描述】:

我正在从事一个学校项目,该项目从 Excel 工作表中提取数据并将数据放入数据库中。在我运行下面的代码并运行 SQL 命令 SELECT * FROM items 后,它返回“空集”。我究竟做错了什么?提前感谢您的帮助!

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

【问题讨论】:

  • 在 cur.execute() 之后尝试 db.commit()

标签: python mysql excel python-3.x mysql-python


【解决方案1】:

您错过了提交,您必须在 INSERT 的末尾提交您的查询

db.commit()

【讨论】:

    猜你喜欢
    • 2013-08-12
    • 1970-01-01
    • 2018-04-25
    • 2021-07-10
    • 1970-01-01
    • 2017-08-17
    • 2013-02-01
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多