Inserting csv into MySQL database with python library mysql.connector - python

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

I'm guessing that seems the problem is that you passed one argument (row) instead of four. So try this:
cursor.execute("""
INSERT INTO flex(date, Store, Vendor, Shelf)
VALUES(%s,%s,%s,%s)""",(row[0], row[1], row[2], row[3], ))

Looking at the documentation for MySQLCursor.excute() method, it seems like adding some %s as the parameters in your insert statement might fix this?
import mysql.connector
import csv
andreport = 'testowa.csv'
cnx = mysql.connector.connect(
user='xxxxx',
password='xxxxx',
host='xxxxxx',
database='xxxxx')
cursor = cnx.cursor()
insert_statement = (
"INSERT INTO flex(date, Store, Vendor, Shelf)"
"VALUES (%s, %s, %s, %s)"
)
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_statement, row)
cnx.commit()
cursor.close()
cnx.close()
print("Done")
Let me know if this gets you anywhere, or if you see a new error!
Edit: updated CSV reading to convert to a list.

Related

Import Data from .csv file into mysql using python

I am trying to import data from two columns of a .csv file (time hh:mm, float). I created a database and a table in mysql.
import mysql.connector
import csv
mydb = mysql.connector.connect(host='127.0.0.1',
user= 'xxx',
passwd='xxx',
db='pv_datenbank')
cursor = mydb.cursor()
# get rid of the '' at the beginning of the .csv file
s = open('Sonneneinstrahlung.csv', mode='r', encoding='utf-8-sig').read()
open('Sonneneinstrahlung.csv', mode='w', encoding='utf-8').write(s)
print(s)
with open('Sonneneinstrahlung.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
sql = """INSERT INTO einstrahlung ('Uhrzeit', 'Einstrahlungsdaten') VALUES (%s, %s)"""
for row in csv_reader:
print(row)
print(cursor.rowcount, "was inserted.")
cursor.executemany(sql, csv_reader)
#cursor.execute(sql, row, multi=True)
mydb.commit()
mydb.close()
If I run the program with executemany(), result is the following:
['01:00', '1']
'-1 was inserted.'
and after this I do get the error code: Not all parameters were used again.
When I try the same thing with the execute() operator, no error is shown, but the data is not inserted in the table of my database.
Here you can see the input data:
executemany takes a statement and a sequence of sets of parameters.
Try this:
with open('Sonneneinstrahlung.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
sql = """INSERT INTO einstrahlung (Uhrzeit, Einstrahlungsdaten) VALUES (%s, %s)"""
cursor.executemany(sql, csv_reader)
mydb.commit()

Unable to import CSV file data to mysql table

I am using the code below-
import mysql.connector
import csv
mydb = mysql.connector.connect(host='xxxxx', user='xxxx', passwd='xxxxxx')
cursor = mydb.cursor()
cursor.execute("SHOW DATABASES")
l = cursor.fetchall()
cursor.execute("USE DB ")
with open('details.csv','rt')as f:
csv_data = csv.reader(f)
for row in csv_data:
cursor.execute ('INSERT INTO student (id,name,age,course) VALUES (%s,%s,%s,%s)',row)
cursor.close()
mydb.commit()
mydb.close()
I am getting the following error-
"ProgrammingError: Not all parameters were used in the SQL statement"
python 3.x,windows7,Mysql 2012 version.
for row in csv_data:
listval = []
for col in row:
listval.append(col)
cursor.execute ('INSERT INTO student (id,name,age,course) VALUES (%s,%s,%s,%s) ', listval)
This will work :))
In this case, the main problem is the string format. I did't know what object is row, in your error it is a list, so using each element will solve the issue.
for row in csv_data:
cursor.execute ('INSERT INTO student (id,name,age,course) VALUES (%s,%s,%s,%s)',
row[0], row[1], row[2], row[3])

Incorrect integer value when insert data to mysql database python

I have sample code like this:
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost', user='root', passwd='root', db='marbola')
cursor = mydb.cursor()
with open('./Team_Attributes.csv') as csv_data:
csv_reader = csv.reader(csv_data)
next(csv_reader, None)
for row in csv_reader:
insert=(
"INSERT INTO Team_Attributes(id,team_fifa_api_id,team_api_id,date,buildUpPlaySpeed,buildUpPlaySpeedClass,buildUpPlayDribbling,buildUpPlayDribblingClass,buildUpPlayPassing,buildUpPlayPassingClass,buildUpPlayPositioningClass,chanceCreationPassing,chanceCreationPassingClass,chanceCreationCrossing,chanceCreationCrossingClass,chanceCreationShooting,chanceCreationShootingClass,chanceCreationPositioningClass,defencePressure,defencePressureClass,defenceAggression,defenceAggressionClass,defenceTeamWidth,defenceTeamWidthClass,defenceDefenderLineClass)"
+ " VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
)
cursor.execute(insert, row[:25])
mydb.commit()
cursor.close()
print("Done")
I used above code to insert data into mysql database from csv file. The csv file is the import result from sqlite database of football analysis from kaggle.com. But there is an error:
_mysql_exceptions.OperationalError: (1366, "Incorrect integer value: '' for column 'buildUpPlayDribbling' at row 1")
This is the csv sample image:
Whats wrong with the code? How to solve this error? I used python 3.5

Populate sqlite3 database from csv: Syntax error around '?'

Following a previous post concerning the population of a sqlite3 database from a csv file in python, I have used the code exactly as written but keep coming up with: Traceback (most recent call last):
File "Z:/KS4/Computer Science/OCR corsework/Task 1 Database/populate.py", line 10, in <module>
cursor.execute(query, data)
sqlite3.OperationalError: near "?": syntax error
This is the code:
import csv, sqlite3
connection = sqlite3.connect("TutorGroup.db")
with open ('studentsEmail-master.csv', 'r') as f:
r = csv.reader(f)
data = next(r)
query = 'insert into dbo.students ({0})'
query = query.format(','.join('?' * len(data)))
cursor = connection.cursor()
cursor.execute(query, data)
for data in reader:
cursor.execute(query, data)
cursor.commit()
https://www.sqlite.org/lang_insert.html
You need the word values in the query:
query = 'insert into dbo.students values ({0})'
# ^^^^^^

Not enough argument for format string while importing csv into MySQL database

Full Traceback:
File "./csvimportdb.py", line 12, in <module>
cursor.execute('''INSERT INTO newsletter_subscriber(id, name, email ) VALUES("%s", "%s", "%s")''', row)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 159, in execute
query = query % db.literal(args)
TypeError: not enough arguments for format string
My Code:
#!/usr/bin/python
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost', user='english', passwd='english', db='english_mez')
cursor = mydb.cursor()
csv_data = csv.reader(file('final.csv'))
for row in csv_data:
cursor.execute('INSERT INTO newsletter_subscriber(id, name, email )' 'VALUES("%s", "%s", "%s")', row[0], row[1], row[2])
#close the connection to the database.
mydb.commit()
cursor.close()
print "Done"
I need to pass row, row, row to make it right but can't do that. How to fix?
Edit: Printing row gives:
['6630\tCarmen Rocche\trocchecarmen#gmail.com\t\t\t\t\t']
['6631\tSuhasini\tkkalva14#hotmail.com\t\t\t\t\t']
['6632\tAmarjeet \tsweetylamba#gmail.com\t\t\t\t\t']
['6633\tFazali Hadi\tshewasb#yahoo.com\t\t\t\t\t']
['6634\tVishaka Samarakone\tshirashi.vishaka#gmail.com\t\t\t\t\t']
['6635\tLoemongga\tloemongga#yahoo.com\t\t\t\t\t']
for row in csv_data:
data=row[0].split('\t')
if len(data) < 4: continue
query="""insert into newsletter_subscriber (id, name, email) values
(%d, '%s', '%s')""" %(int(data[0]), data[1], data[2])
cursor.execute(query)

Categories