[Fixed]-Load CSV values then search for matches in mysql database and then apply insertion?

1👍

Maybe you can do like this:

import csv
import pymysql

path = '/home/abi/Downloads'

conn = pymysql.connect(host='127.0.0.1', port=3306, user='user',                 
passwd='pass', db='db_name', charset='utf8')
cursor1 = conn.cursor()
cursor2 = conn.cursor()
cursor3 = conn.cursor()

file=open( path +"university.CSV", "r")
reader = csv.reader(file)
for line in reader:
   csv_school_name = line[0]
   csv_school_type = line[3]
   cursor1.execute("select id from schools_names where school_name = s%", csv_school_name)
   corresponding_school_name_id = cursor1.fetchone()
   cursor2.execute("select id from schools_types where school_type = s%", csv_school_type)
   corresponding_school_type_id = cursor2.fetchone()

After this you can get school_name_id and school_type_id, then you can insert this two ids into table if you want to do.
I hope this can help you.

you must execute

cursor1.execute("select id from schools_names where school_name = s%", csv_school_name)

ont time, then use fetchone(), if you use

school_id = cursor1.execute("select id from schools_names where school_name = s%", csv_school_name)

you will get count of table.

👤Frank

Leave a comment