简体   繁体   中英

MySQL Insert / update from python, data from excel spreadsheet

Upsert to MySQL using python and data from excel.

Im working on populating a MySQL DB, using python.

The data is stored on excel sheets.

Because the DB is suppossed to be used for monitoring "projects", there's a posibility for repeated pk, so in that case it need to be updated instead of insert, because a project can have many stages.

Also, there's a value to be inserted in the DB table, that can't be added from the spreadsheet. So i'm wondering if in that case, the insert of this value, most be done using a separated query for it or if theres a way to insert it in the same query. The value is the supplier ID and needs to be inserted between id_ops and cif_store.

And to finish, I need to perform an inner join, to import the store_id using the store_cif, from another table called store. I know how do it, but im wondering if it also must be executed from a sepparated query or can be performed at the sameone.

So far, i have done this.

import xlrd
import MySQLdb


def insert():

    book = xlrd.open_workbook(r"C:\Users\DevEnviroment\Desktop\OPERACIONES.xlsx")
    sheet = book.sheet_by_name("Sheet1")



    database = MySQLdb.connect (host="localhost", user = "pytest", passwd = "password", db = "opstest1")


    cursor = database.cursor()


    query = """INSERT INTO operation (id_ops, cif_store, date, client, 
    time_resp, id_area_service) VALUES (%s, %s, %s, %s, %s, %s)"""


    for r in range(1, sheet.nrows):
        id_ops = sheet.cell(r,0).value
        cif_store = sheet.cell(r,1).value
        date = sheet.cell(r,2).value
        client = sheet.cell(r,3).value
        time_resp = sheet.cell(r,4).value
        id_area_service = sheet.cell(r,5).value


        values = (id_ops, cif_store, date, client, time_resp, id_area_service)


        cursor.execute(query, values)

# Close the cursor
    cursor.close()

# Commit the transaction
    database.commit()

# Close the database connection
    database.close()

# Print results
    print ("")
    print ("")
    columns = str(sheet.ncols)
    rows = str(sheet.nrows)
    print ("Imported", columns,"columns and", rows, "rows. All Done!")
insert()

What you are looking for is INSERT ... ON DUPLICATE KEY UPDATE ...

Take a look here https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

Regarding the extraneous data, if its a static value for all rows you can just hard code it right into the INSERT query. If it's dynamic you'll have to write some additional logic.

For example:
query = """INSERT INTO operation (id_ops, hard_coded_value, cif_store, date, client, time_resp, id_area_service) VALUES (%s, "my hard coded value", %s, %s, %s, %s, %s)"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM