Importing data from CSV to MySQL using python - python

I am currently working on a schoolproject, and im trying to import data from a CSV file to MySQL using python. This is my code so far:
import mysql.connector
import csv
mydb = mysql.connector.connect(host='127.0.0.1', user='root', password='abc123!', db='jd_university')
cursor = mydb.cursor()
with open('C:/Users/xxxxxx/Downloads/Students.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
cursor.execute('INSERT INTO Student (First_Name, Last_Name, DOB, Username, Password, Phone_nr,'
'Email, StreetName_nr, ZIP) '
'VALUES("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")',
row)
mydb.commit()
cursor.close()
When i run this, i get this error: "mysql.connector.errors.DataError: 1292 (22007): Incorrect date value: '%s' for column 'DOB' at row 1"
The date format used in the CSV file are yyyy-mm-dd
Any tips on this would help greatly!

You don't need to quote the %s placeholders.
Since you're using DictReader, you will need to name the columns in your row expression (or not use DictReader and hope for the correct order, which I'd not do).
Try this:
import mysql.connector
import csv
mydb = mysql.connector.connect(
host="127.0.0.1", user="root", password="abc123!", db="jd_university"
)
cursor = mydb.cursor()
with open("C:/Users/xxxxxx/Downloads/Students.csv") as csvfile:
reader = csv.DictReader(csvfile, delimiter=",")
for row in reader:
values = [
row["First_Name"],
row["Last_Name"],
row["DOB"],
row["Username"],
row["Password"],
row["Phone_nr"],
row["Email"],
row["StreetName_nr"],
row["ZIP"],
]
cursor.execute(
"INSERT INTO Student (First_Name, Last_Name, DOB, Username, Password, Phone_nr,"
"Email, StreetName_nr, ZIP) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
values,
)
mydb.commit()
cursor.close()

Validate the datatype for DOB field in your data file and database column. Could be a data issue or table definition issue.

Related

AttributeError 'DictCursor' object has no attribute 'update'

I'm trying to import some CSV files to a table on a MySQL database. The CSV files are updated daily and my intention is to use this program in python to automate the process.
The idea is: if the information already exists, I want to update it. If the information doesn't exist, I want to insert the data.
But I'm having this error:
AttributeError
'DictCursor' object has no attribute 'update'
Thanks in advance.
csv_data = csv.reader(open('ATEG_REGIONAL_MG_DADOS_TB_ATIVIDADE.csv', encoding='ISO-8859-15'), delimiter=';')
next(csv_data)
for row in csv_data:
for i, l in enumerate(row):
if row[i] == '':
row[i] = None
cursor.execute('SELECT * FROM atividade WHERE CD_ATIVIDADE=%s', row[0])
if cursor.fetchall():
cursor.update('UPDATE atividade WHERE CD_ATIVIDADE = row[0]'),
else:
cursor.execute('INSERT INTO atividade (CD_ATIVIDADE, NM_ATIVIDADE, ST_ATIVO, COD_USUARIO_INCLUSAO, COD_USUARIO_ALTERACAO, DAT_INCLUSAO, DAT_ALTERACAO, CO_ATIVIDADE_REZOLVE, ROWID, FLG_SAFRA, FLG_PRODUTIVO, FLG_TIPO_ATIVIDADE, FLG_INDICADOR_ISA) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', row)
# close the connection to the database.
db.commit()
cursor.close()
print("Imported!")
If you are using psycopg2, there is no cursor.update() function present. Try cursor.execute() instead.
Also row[0] is considered as a string in your query. So, change it to:
cursor.execute('UPDATE atividade WHERE CD_ATIVIDADE = ' + row[0])
Seems like you are confusing two different libraries.
import MySQLdb and from flask_mysqldb import MySQL are two different libraries.
Since you are using flask adding this line app.config['MYSQL_CURSORCLASS'] = 'DictCursor' and then calling the cursor cursor=db.connection.cursor() should solve your problem.
Taken from the official git page

importing values of a python dictionary as data to an existing mysql table

I have problem with storing values of a python dictionary as data to an existing mysql table
I tried to use the code below but it's not working.
db = mysql.connect(
host="localhost",
user="root",
passwd="123456",
database="tgdb"
)
cursor = db.cursor()
val = ', '.join("'" + str(x) + "'" for x in dict.values())
sql = "INSERT INTO tgdb.channel(user_name, image_url, name,
number_of_members, description, channel_url) VALUES (%s, %s, %s, %s, %s,
%s)"
cursor.execute(sql, val)
db.commit()
print(cursor.rowcount, "record inserted.")
"you have an error in your SQL syntax"
As writed #Torxed shouldn't translate dict in string, you can write just that:
cursor.execute(sql, list(dict.values())

mysql.connector in python / cursor in for-loop only working for first row of table

I have a table and I want to translate columns 'topic' and 'review' of a row and store the entire table with their translations into a new table. It seems that the for-loop doesn't iterate over all rows of the input table. Only the first row is stored into the new table. Why?
database = mysql.connector.connect(user='root', password='root', host='localhost', database='test')
DBcursor = database.cursor(buffered=True)
query = ("SELECT * FROM test_de")
DBcursor.execute(query)
for (id, user_name, date, country, version, score, topic, review, url) in DBcursor:
topic_trans = translate(topic, 'en')
review_trans = translate(review, 'en')
add_translation = ("INSERT INTO test_de_en(id, user_name, date, country, version, score, topic, review, url)"
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
translation_data = (id, user_name, date, country, version, score, topic_trans, review_trans, url)
DBcursor.execute(add_translation, translation_data)
database.commit()
DBcursor.close()
database.close()

Unable to iteratively insert values in Python-MySQL tables

I am facing error while trying to iteratively insert values in MySQL table. I am not sure if the insert statement is correct? or do we have any better way of inserting values in my table.
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax;
import mysql.connector
from collections import defaultdict
cnx = mysql.connector.connect(user='root', password='hadoop',
host='localhost',
database='test')
lines = defaultdict(dict)
with open("test.txt") as f:
for line in f:
parts = line.split()
key = tuple(parts[1:3]) # unique key with servername and datetime
lines[key][parts[0]] = parts[3]
lines[key]["servername"] = parts[2]
lines[key]["datetime"] = parts[1]
res = list(lines.values())
try:
cursor = cnx.cursor()
for index in range(len(res)):
cursor.execute("""
insert into cpu_util_all (servername,date_time,cpu_number,cpu_user,cpu_nice,cpu_system,cpu_wait,cpu_idle) values ('%s', '%s', '%s', '%s','%s', '%s', '%s', '%s') % (res[index]["servername"],res[index]["datetime"],res[index]["cpunumber"],res[index]["cpuuser"],res[index]["cpunice"],res[index]["cpusystem"],res[index]["cpuwait"],res[index]["cpudile"])
""")
cnx.commit()
cursor.close()
finally:
cnx.close()
print("Inserted successfully")
The problem is that you're trying to do string substitution in the query itself. Further, you should allow MySQLdb to parse the parameter values for you, e.g.:
cursor.execute("""
insert into cpu_util_all
(servername,date_time,cpu_number,cpu_user,
cpu_nice,cpu_system,cpu_wait,cpu_idle)
values (%s, %s, %s, %s, %s, %s, %s, %s)""",
(res[index]["servername"],res[index]["datetime"],
res[index]["cpunumber"],res[index]["cpuuser"],
res[index]["cpunice"],res[index]["cpusystem"],
res[index]["cpuwait"],res[index]["cpudile"]
)
)

CSV data to two MYSQL tables using Python

I need to take data from a csv file and import it into two mysql tables within the same database.
CSV file:
username,password,path
FP_Baby,7tO0Oj/QjRSSs16,FP_Baby
lukebryan,uu00U62SKhO.sgE,lukebryan
saul,r320QdyLJEXKEsQ,saul
jencarlos,LOO07D5ZxpyzMAg,jencarlos
abepark,HUo0/XGUeJ28jaA,abepark
From the CSV file
username and password go into the USERS table
path goes into VFS_PERMISSIONS table
The USERS table looks like
INSERT INTO `USERS` (`userid`, `username`, `password`, `server_group`) VALUES
(23, 'username', 'password', 'MainUsers'),
INSERT INTO `VFS_PERMISSIONS` (`userid`, `path`, `privs`) VALUES
(23, '/path/', '(read)(write)(view)(delete)(resume)(share)(slideshow)(rename)(makedir)(deletedir)'),
if possible I'd like to start the userid in both tables at 24 and increment +1 for each row in the csv.
SO far I can read the csv files but I can't figure out how to insert into two mysql tables.
#!/usr/bin/env python
import csv
import sys
import MySQLdb
conn = MySQLdb.connect(host= "localhost",
user="crushlb",
passwd="password",
db="crushlb")
x = conn.cursor()
f = open(sys.argv[1], 'rt')
try:
reader = csv.reader(f)
for row in reader:
## mysql stuff goes here right?
finally:
f.close()
You can reduce the number of calls to cursor.execute by preparing the arguments in advance (in the loop), and calling cursor.executemany after the loop has completed:
cursor = conn.cursor()
user_args = []
perm_args = []
perms = '(read)(write)(view)(delete)(resume)(share)(slideshow)(rename)(makedir)(deletedir)'
with open(sys.argv[1], 'rt') as f:
for id, row in enumerate(csv.reader(f), start = 24):
username, password, path = row
user_args.append((id, username, password, 'MainUsers'))
perm_args.append((id, path, perms))
insert_users = '''
INSERT IGNORE INTO `USERS`
(`userid`, `username`, `password`, `server_group`)
VALUES (%s, %s, %s, %s)
'''
insert_vfs_permissions = '''
INSERT IGNORE INTO `VFS_PERMISSIONS`
(`userid`, `path`, `privs`)
VALUES (%s, %s, %s)
'''
cursor.executemany(insert_users,user_args)
cursor.executemany(insert_vfs_permissions,perm_args)
INSERT IGNORE tells MySQL to try to insert rows into the MySQL table, but ignore the command if there is a conflict. For example, if userid is the PRIMARY KEY, and there is already a row with the same userid, then the INSERT IGNORE SQL will ignore the command to insert a new row since that would create two rows with the same PRIMARY KEY.
Without the IGNORE, the cursor.executemany command would raise an exception and fail to insert any rows.
I used INSERT IGNORE so you can run the code more than once without cursor.executemany raising an exception.
There is also a INSERT ... ON DUPLICATE KEY UPDATE command which tells MySQL to try to insert a row, but update it if there is a conflict, but I'll leave it at this unless you want to know more about ON DUPLICATE KEY.
Since you already know the sql statements that you wan to execute, it should be more or less straightforward to use the cursor.execute method:
offset = 23
for row_number, row in enumerate(reader):
username, password, path = row
x.execute("INSERT INTO `USERS` (`userid`, `username`, `password`, `server_group`) "
"VALUES (%s, %s, %s, 'MainUsers')", (row_number+offset, username, password))
x.execute("INSERT INTO `VFS_PERMISSIONS` (`userid`, `path`, `privs`) "
"VALUES (%s, %s, '(read)(write)(view)(delete)(resume)(share)(slideshow)(rename)(makedir)(deletedir)'", (row_number+offset, path))

Categories

Resources