jeudi 14 janvier 2016

Python SQLite3 database

I am trying to parse JSON file and then import the content to my database, I am using sqlite3 but I get this error:

line 15, in <module>
    curs.execute("INSERT INTO Subreddit VALUES (?,?,?)", (po['subreddit'], po['name'], po['subreddit_id'],))
sqlite3.IntegrityError: UNIQUE constraint failed: Subreddit.subreddit

and here is my code:

import json
import bz2
import sqlite3

fpath = "/home/taner/PycharmProjects/DatabaseA1/RC_2007-10.bz2"
databasepath = "/home/taner/reddit"

conn = sqlite3.connect(databasepath)
curs = conn.cursor()
with bz2.BZ2File(fpath) as file:
    for line in file:
        dummy_json = line
        json_object = json.loads(dummy_json)
        po = json.loads(line.decode('utf8'))
        curs.execute("INSERT INTO Subreddit VALUES (?,?,?)", (po['subreddit'], po['name'], po['subreddit_id'],))
        conn.commit()

Aucun commentaire:

Enregistrer un commentaire