0

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")
1

2 Answers 2

1

Because allow_local_infile is True you can use LOAD DATA LOCAL INFILE'filename' INTO TABLE jeremy_table_test to easily load CSV files.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.