I am having issues importing a CSV file into a MySql Table with the python script below; when attempting to import the file 101321_Odr.csv
As can be seen -- All the data fields imports correctly except for the Alloy Field --(Grabs data from the next line) - Note: Have same results if i delete the Index Field.
Table structure can be seen below:
Output from from my print statement --- print (load_sql) is
LOAD DATA LOCAL INFILE 'C:Test/101321_Odr.csv' INTO TABLE timerite.tbl_orders FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES;
Any thoughts -- let me know
Thanks - Dan
#import csv
import mysql.connector
import sys
import pymysql
from time import strftime
import datetime
import logging
import subprocess #used to mapp drive
import smtplib
host = '10.X.X.X'
user = 'user'
password = 'password'
def csv_to_mysql(load_sql, host, user, password):
'''
Function loads a csv file to MySQL table according to
'''
try:
con = pymysql.connect(host=host,
user=user,
password=password,
autocommit=True,
local_infile=1)
print('Connected to DB: {}'.format(host))
cursor = con.cursor()
cursor.execute(load_sql)
print('Succuessfully loaded the table from csv.')
con.close()
except Exception as e:
print('Error: {}'.format(str(e)))
# logger.debug('Error: {}'.format(str(e)))
sys.exit(1)
# SET GLOBAL local_infile = true
print ("Allow InFile Import ----- SET GLOBAL local_infile = true")
load_sql = "SET GLOBAL local_infile = true;"
print (load_sql)
csv_to_mysql(load_sql, host, user, password)
# Delete Data from tables
print ("Deleting Data from Tables ")
load_sql = "DELETE FROM timerite.tbl_orders;"
print (load_sql)
csv_to_mysql(load_sql, host, user, password)
print(("Table Data has been deleted"))
# Import CSV File to table
strDate = strftime("%m%d%y")
strDate = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%m%d%y")
print((strDate))
strFileOdr = "'" + "C:Test/" + strDate + "_Odr.csv'"
print("Order File is: " + strFileOdr)
load_sql = "LOAD DATA LOCAL INFILE " + strFileOdr + " INTO TABLE timerite.tbl_orders FIELDS TERMINATED BY ',' ENCLOSED BY '\"' IGNORE 1 LINES;"
print (load_sql)
csv_to_mysql(load_sql, host, user, password)
print("All Tables have been Updated")
ASKER
ASKER
from time import strftime
import datetime
strDate = strftime("%m%d%y")
strDate = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%m%d%y")
strFileOdr = f"C:/Test/{strDate}_Odr.csv"
load_sql = f"""LOAD DATA
LOCAL INFILE '{strFileOdr}'
INTO TABLE timerite.tbl_orders
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES;
"""
print(f"Order File is:\n{strFileOdr}\n")
print(f"LOAD SQL:\n{load_sql}\n")
LINES TERMINATED BY '\n' STARTING BY ''
or the double quote we see is not the default double quote, but some Unicode characters.
MySQL is an open source, relational database management system that runs as a server providing multi-user access to a number of databases. Acquired by Oracle in 2009, it is frequently used in combination with PHP installations, powering most of the WordPress installations.
TRUSTED BY
To escape the commas in the field, the field is enclosed by quotes" in your case, a quote is messed up.
The entire cell should be enclosed, not a portion.