I am currently undertaking an assignment for university. I've having quite a bit of trouble with this seemingly simple task, which is to import the data from 2 .txt files into 2 tables within an sql database via python.
Here is what I've done so far. This subject is a core unit in my course, which is a double degree with business. I mention this merely to make you aware that programming is not my strong suit.
import MySQLdb def connect_to_database(user, password): return MySQLdb.connect(host='localhost', user='root', passwd='0dy5seuS') ###Load text files containing car details into variables car_details = file(car_details.txt) cars_for_sale = file(cars_for_sale.txt) cursor.execute('USE cars_db') #Define Car Details for implementation def load_cd_file(): Load_CD = ('LOAD DATA LOCAL INFILE 'car_details.txt' INTO TABLE car_details FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\r\n') #Define Cars for sale for implementation def load_cfs_file(cursor, *files): Load_CFS = ('LOAD DATA LOCAL INFILE 'cars_for_sale.txt';' INTO TABLE cars_for_sale FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\r\n') cursor.execute(Load_CD, Load_CFS) #define data entry function def data_entry(cars_db): #Make a connection to the mySQL database load files(cursor, ('car_details.txt'), ('cars_for_sale.txt')) db.commit() cursor.close() data_entry() db.close()
I'm confident that there are plenty of mistakes within, but I'd be very grateful for any help you can provide.