I'm using Postgresql 10 and I need to do a lookup in the CSV file and compare the entries in the CSV file with the entries in my postgres table. The database looks likes this, where I have to insert the domain name in domains table and ranks in ranks table:
CREATE TABLE lists (list_id integer PRIMARY KEY,
list_name text);
CREATE TABLE domains (domain_id BIGSERIAL PRIMARY KEY,
domain_name text UNIQUE);
CREATE TABLE ranks (list_id integer REFERENCES lists,
domain_id integer REFERENCES domains,
rank integer,
date date,
PRIMARY KEY (list_id, rank, date));
The csv contains two entries, a rank and a domain name like this: "1, google.com"
Currently I insert the domainnames into the domain table, where the domain id is auto incremented and serves as a primary key. Then I want to insert the ranks into the ranks table. But I'm struggeling to get the domain_id from the domains table into the ranks table as the domain_id serves as a foreign key in the ranks table. So I want to check the CSV for the domain name, check it up against the domains table and get out the domain_id for each domain as i insert the ranks. So each domain name can have several ranks, this is made distinct by the date.
The current script I'm using now looks like this:
import tkinter as tk
from tkinter import filedialog
import csv
import psycopg2
import shutil as sh
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
new_path = 'C:/Users/%user%/Desktop/alexa-top1m_16042018.csv'
conn = psycopg2.connect("host=localhost dbname=test user=postgres password=test")
cur = conn.cursor()
sh.copy2(file_path, new_path)
with open(new_path, 'r') as original: data = original.read()
with open(new_path, 'w') as modified: modified.write("rank,domain_name\n" + data)
with open(new_path, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
cur.execute(
"""INSERT INTO ranks (list_id, rank, date) VALUES (%s, %s, %s);""", ( 1, row['rank'], '2018-04-16',)
)
conn.commit()
Im using psycopg2 to connect to the DB and make queries.
Do anyone know how to do this, or have any other suggestions on how to achieve this?