1

I am using Python and a MySQL database and am attempting to itterate through rows in a CSV file and insert them in my database. I have the following:

import mysql.connector
import pandas as pd

mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="root",
    database="mydb")

cursor = mydb.cursor()
cursor.execute("SET FOREIGN_KEY_CHECKS=0")
csv_data = pd.read_csv("file path")
sql = "INSERT INTO table (ID, SecondID, StartDate, EndDate) VALUES (%s, %s, %s, %s)"
for index, row in csv_data.iterrows():
    cursor.execute(sql, row)
cursor.execute("SET FOREIGN_KEY_CHECKS=1")
mydb.commit()
cursor.close()
mydb.close()

I can't see what's wrong with the SQL. Getting the following error:

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 '%s, %s, %s, %s)'

NOTE - The rest of the code seems to work okay and the SQL works fine if I insert specific values but when I try to use the %s construct it fails yet other responses I have seen appear to recommend this as the correct syntax.

Please help- what am I doing wrong?

6
  • It's worth checking what parameter style you're using. Commented Oct 4, 2020 at 9:02
  • 1
    Is your table really called "table"? Is that allowed? Commented Oct 4, 2020 at 9:06
  • anyway, why there are %s without any variables ? eg "%s"%(string) Commented Oct 4, 2020 at 9:08
  • @sygneto That is how statement parameters work. Commented Oct 4, 2020 at 9:08
  • 1
    If your table really named table, the table name must be quoted by backtics Commented Oct 4, 2020 at 9:14

3 Answers 3

1

I think you better use pandas to_sql function.
I'm not sure whether mysql.connector works so i'll use sqlalchemy.
It looks like that:

ENGINE = sqlalchemy.create_engine('mysql+pymysql://root:root@localhost:3306/mydb')
with ENGINE.connect() as connection:
    ENGINE.execute("SET FOREIGN_KEY_CHECKS=0")
    csv_data.to_sql('table_name', connection, if_exists='append', index=False)
    ENGINE.execute("SET FOREIGN_KEY_CHECKS=1")

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

Comments

0

Look like that the problem is that you are invoking the query without scape the value.

The execute function its getting a class and not an array

for index, row in csv_data.iterrows():
    cursor.execute(sql, row)

You should generate an array with all the values and then invoke the query.

Something like:

for index, row in csv_data.iterrows():
    params = map(lambda x : x.value, row)
    cursor.execute(sql,params)

Be carefull, the size of the array has to be the same size as the values params. In this case 4

Comments

0

Thank you that was very helpful, I made one minor change and it works perfectly now. Here is the final solution I used:

import pandas as pd
import sqlalchemy

engine = sqlalchemy.create_engine('mysql+pymysql://root:root@localhost:3306/mydb')

csv_data = pd.read_csv("file path")
engine.execute("SET FOREIGN_KEY_CHECKS=0")

with engine.connect() as connection:
    csv_data.to_sql('table', connection, if_exists='append', index=False)

engine.execute("SET FOREIGN_KEY_CHECKS=1")

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.