I want to insert my array value result =['2018-10-18_trx_result-reuslt.csv']
to my column date_sourced
, but my problem is how can I include this to my query for rows in my csv data.
Here is my code to import my csv to mysql table
for now, my date_sourced
is 0000-00-00
, so I want to replace it by my result
variable
import csv
import mysql.connector
import re
result =['2018-10-18_trx_result-reuslt.csv']
mydb = mysql.connector.connect(user='root', password='',
host='localhost',
database='jeremy_db')
file = open('C:\\Users\\trendMICRO\\Desktop\\OJT\\test.csv', 'rb')
csv_data = csv.reader(file)
cursor = mydb.cursor()
for row in csv_data:
cursor.execute('INSERT INTO jeremy_table_test(date_sourced, sha1, vsdt,trendx,notes )' 'VALUES(%s, %s, %s, %s,%s)',row)
mydb.commit()
cursor.close()
print("Done")
由于allow_local_infile为True
您可以使用LOAD DATA LOCAL INFILE 'filename' INTO TABLE jeremy_table_test
轻松加载 CSV 文件。
Read here: https://dev.mysql.com/doc/refman/8.0/en/load-data.html
You can do:
LOAD DATA LOCAL INFILE'filename' INTO TABLE jeremy_table_test
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.