Inserting csv into MySQL database with python library mysql.connector

2019-12-08T04:29:42

I have trouble with insert of csv data into MySQL tabel with mysql.connector .

The code I use looks like this :

import mysql.connector
import csv

andreport = 'testowa.csv'
cnx = mysql.connector.connect(
    user='xxxxx',
    password='xxxxx',
    host='xxxxxx',
    database='xxxxx')

cursor = cnx.cursor()

with open(andreport, 'r') as csv_data:
    for row in csv_data:
        cursor.execute(
            "INSERT INTO flex(date, Store, Vendor, Shelf)"
            "VALUES({},{},{},{})", row)
cnx.commit()
cursor.close()
cnx.close()
print("Done")

The error I get :

C:\Users\Iw4n\PycharmProjects\Learning\venv\Scripts\python.exe C:/Users/Iw4n/PycharmProjects/Learning/Orange_android_MySQL_insertion.py
Traceback (most recent call last):
  File "C:/Users/Iw4n/PycharmProjects/Learning/Orange_android_MySQL_insertion.py", line 15, in <module>
    cursor.execute(
  File "C:\Users\Iw4n\PycharmProjects\Learning\venv\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Users\Iw4n\PycharmProjects\Learning\venv\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Users\Iw4n\PycharmProjects\Learning\venv\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '},{},{},{})' at line 1

When i wrapped {} into '' , as many rows as were in csv been inserted into datbase as {},{}

same story goes for %s if I use it , i got the same error as above, when it's wrapped in '' , %s is insetred into database. I also found information to add f in fron of "INSERT~ but it did not help.

Can anyone give me some suggestion on how to overcome this and correctly insert data to MySQL ?

Final code that is working as intended :

import mysql.connector
import csv

andreport = 'testowa.csv'
cnx = mysql.connector.connect(
    user='xxxxx',
    password='xxxxx',
    host='xxxxx',
    database='xxxxx')

cursor = cnx.cursor()

with open(andreport, mode='r') as csv_data:
    reader = csv.reader(csv_data, delimiter=';')
    csv_data_list = list(reader)
    for row in csv_data_list:
        cursor.execute("""
                   INSERT INTO flex(
                   date, Agency, MediaSource, Campaign)
                   VALUES(%s,%s,%s,%s)""",
                    (row[0], row[1], row[2], row[3]))
cnx.commit()
cursor.close()
cnx.close()
print("Done")

Copyright License:
Author:「Iw4n」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/59229903/inserting-csv-into-mysql-database-with-python-library-mysql-connector

About “Inserting csv into MySQL database with python library mysql.connector” questions

I have trouble with insert of csv data into MySQL tabel with mysql.connector . The code I use looks like this : import mysql.connector import csv andreport = 'testowa.csv' cnx = mysql.connector.
As described in the title, I have a problem with inserting emojis into a MariaDB database using the mysql.connector module in python. When I try to insert the data containing emojis into the database
I have a csv file with the number of people infected by covid19 per country and per day. I have created a MYSQL database, a table with all the columns that the CSV file has and now I need to insert...
I use mysql.connector library in Python to send query to database. But, when the database is changed after the initialization, the mysql.connector’s tools answer like if the database had never chan...
I am new to python.I want to connect to mysql using the mysql.connector with python.After i want to open a csv and then upload it to mysql database.I am using python version : 3.6.0 I have tried t...
I am developing an app which uses MariaDB and mysql.connector for python. However, I reached to some point where I see that PostgreSQL could have benefits (e.g. it exists RETURNING). If I create ag...
I'm connecting to the Facebook Graph API using Python and the curl response delivers a bunch of data in Unicode format. I am trying to insert this data into a mysql database using the python mysql.
I want to load the following file from my local server into a database on a remote server: /Users/user/Documents/test.csv: a,b,c 1,2,3 4,5,6 I have the following statements: drop table if exists
I have been using the mysql.connector module with Python 2.7 and testing locally using XAMPP. Whenever I upload my script to the server, I am getting an import error for the mysql.connector module...
I'm trying to import csv format into mysql using Spyder, then from the sql data to plot a graph using pandas (this is a work project). This is my code: import mysql.connector,sys, pandas as pd i...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.