I have 2 .txt files, and I converted them into .csv files using https://convertio.co/csv-xlsx/. Now, I would like to import these two .csv files into two databases using SQLite in Python (UI is Jupyter Notebook). These two .csv files are labeled person.csv and person_votes.csv. So, I did it by following the code given here (Importing a CSV file into a sqlite3 database table using Python):
import sqlite3, csv
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE person (personid STR,age STR,sex STR,primary_voting_address_id STR,state_code STR,state_fips STR,county_name STR,county_fips STR,city STR,zipcode STR, zip4 STR, PRIMARY KEY(personid))")
with open('person.csv','r') as person_table: # `with` statement available in 2.5+
# csv.DictReader uses first line in file for column headings by default
dr = csv.DictReader(person_table) # comma is default delimiter
#personid age sex primary_voting_address_id state_code state_fips county_name county_fips city zipcode zip4
to_db = [(i['personid'], i['age'], i['sex'], i['primary_voting_address_id'], i['state_code'], i['state_flips'], i['county_name'], i['county_fips'], i['city'], i['zipcode'], i['zip4']) for i in dr]
cur.executemany("INSERT INTO t (age, sex) VALUES (?, ?);", to_db)
con.commit()
I don't understand why when I tried executing the code above, I keep getting the error message: "KeyError: 'personid'". Could someone please help?
Also, if I create another database table named to_db2 for the file person_votes.csv in the same Python file, would the following query give me all the common elements between two tables:
select ID from to_db, to_db2 WHERE to_db.ID == to_db2
The link to the two .csv files above is here: https://drive.google.com/open?id=0B-cyvC6eCsyCQThUeEtGcWdBbXc.