import csv, psycopg2, pwinput from tqdm import tqdm host = input('host: ') user = input('user: ') password = pwinput.pwinput(prompt='password: ') dbname = input('dbname: ') port = int(input('port: ')) conn = psycopg2.connect(host=host, user=user, password=password, dbname=dbname, port=port) cur = conn.cursor() cur.execute('CREATE TABLE IF NOT EXISTS tasks (id integer NOT NULL, correct_answer character(5), media_url text, weight smallint);') cur.execute('CREATE TABLE IF NOT EXISTS questions (task_id integer, lang character(2), text text);') cur.execute('CREATE TABLE IF NOT EXISTS tasks_advanced (id integer NOT NULL, correct_answer character(1), media_url text, weight smallint);') cur.execute('CREATE TABLE IF NOT EXISTS questions_advanced (task_id integer, lang character(2), text text, answer_a text, answer_b text, answer_c text);') cur.execute('CREATE TABLE IF NOT EXISTS categories (name text, task_id integer);') cur.execute('TRUNCATE TABLE tasks;') cur.execute('TRUNCATE TABLE questions;') cur.execute('TRUNCATE TABLE categories;') cur.execute('TRUNCATE TABLE tasks_advanced;') cur.execute('TRUNCATE TABLE questions_advanced;') with open('pytania.csv') as file: reader = csv.reader(file, delimiter=';') for row in tqdm(reader): numer = row[0] pytanie_pl = row[1] odp_a_pl = row[2] odp_b_pl = row[3] odp_c_pl = row[4] poprawna_odp = row[5] media = row[6] kategorie = row[7] pjm_pytanie = row[8] pjm_odp_a = row[9] pjm_odp_b = row[10] pjm_odp_c = row[11] pytanie_en = row[12] odp_a_en = row[13] odp_b_en = row[14] odp_c_en = row[15] pytanie_de = row[16] odp_a_de = row[17] odp_b_de = row[18] odp_c_de = row[19] pytanie_ua = row[20] odp_a_ua = row[21] odp_b_ua = row[22] odp_c_ua = row[23] categories = kategorie.split(',') task_id = int(numer) if numer and pytanie_pl and poprawna_odp and kategorie and not odp_a_pl and not odp_b_pl and not odp_c_pl: if poprawna_odp == 'Tak': correct_answer = 'true' elif poprawna_odp == 'Nie': correct_answer = 'false' else: raise Exception('Error, an answer to a basic question does not equal "Tak" or "Nie", check the pytania.csv file for anomaly in poprawna_odp section') cur.execute('INSERT INTO tasks (id, correct_answer, media_url) VALUES (%s, %s, %s)', (task_id, correct_answer, media)) cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)', (task_id, 'PL', pytanie_pl)) if pytanie_en: cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)', (task_id, 'EN', pytanie_en)) if pytanie_de: cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)', (task_id, 'DE', pytanie_de)) if pytanie_ua: cur.execute('INSERT INTO questions(task_id, lang, text) VALUES (%s, %s, %s)', (task_id, 'UA', pytanie_ua)) if numer and pytanie_pl and poprawna_odp and kategorie and odp_a_pl and odp_b_pl and odp_c_pl: cur.execute('INSERT INTO tasks_advanced(id, correct_answer, media_url) VALUES (%s, %s, %s)', (task_id, poprawna_odp, media)) cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)', (task_id, 'PL', pytanie_pl, odp_a_pl, odp_b_pl, odp_c_pl)) if odp_a_en and odp_b_en and odp_c_en: cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)', (task_id, 'EN', pytanie_en, odp_a_en, odp_b_en, odp_c_en)) if odp_a_de and odp_b_de and odp_c_de: cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)', (task_id, 'DE', pytanie_de, odp_a_de, odp_b_de, odp_c_de)) if odp_a_ua and odp_b_ua and odp_c_ua: cur.execute('INSERT INTO questions_advanced(task_id, lang, text, answer_a, answer_b, answer_c) VALUES (%s, %s, %s, %s, %s, %s)', (task_id, 'UA', pytanie_ua, odp_a_ua, odp_b_ua, odp_c_ua)) for category in categories: cur.execute('INSERT INTO categories(name, task_id) VALUES (%s, %s)', (category, task_id)) with open('punkty.csv') as file: reader = csv.reader(file, delimiter=';') for row in tqdm(reader): numer = int(row[0]) punkty = int(row[1]) cur.execute('UPDATE tasks SET weight=%s WHERE id=%s', (punkty, numer)) cur.execute('UPDATE tasks_advanced SET weight=%s WHERE id=%s', (punkty, numer)) cur.execute('SELECT COUNT(id) FROM tasks') print(f'Imported {cur.fetchone()[0]} tasks') cur.execute('SELECT COUNT(*) FROM questions') print(f'Imported {cur.fetchone()[0]} questions') cur.execute('SELECT COUNT(id) FROM tasks_advanced') print(f'Imported {cur.fetchone()[0]} tasks_advanced') cur.execute('SELECT COUNT(*) FROM questions_advanced') print(f'Imported {cur.fetchone()[0]} questions_advanced') cur.execute('SELECT COUNT(*) FROM categories') print(f'Imported {cur.fetchone()[0]} categories') conn.commit() cur.close() conn.close()