17

In order to prevent my database from growing too large I want sqlite only to insert values that has not yet been inserted. I've done some searching and figured the best way to do so was to use a UNIQUE constraint. It seems to me that that sqlite crashes when inserting a value that is not UNIQUE, how do I circumvent this error and continue with the next submission?

Below is some relevant code.

sql = sqlite3.connect('submissions.db')
cur = sql.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS some_table(id TEXT UNIQUE)')
sql.commit()

for thing in things:
    try:
        # Do some stuff
    except AttributeError:
        pass

    cur.execute('INSERT INTO some_table VALUES(?)', [thing])
    sql.commit()

Here is the traceback:

Traceback (most recent call last):
  File "D:\Directory\Python\Projects\Oddshotcrawler for Reddit, globaloffensive\oddshotcrawler.py", line 62, in <module>
    oddshotcrawler()
  File "D:\Directory\Python\Projects\Oddshotcrawler for Reddit, globaloffensive\oddshotcrawler.py", line 54, in oddshotcrawler
    cur.execute('INSERT INTO oldposts VALUES(?)', [thing])
sqlite3.IntegrityError: UNIQUE constraint failed: some_table.id
[Finished in 7.1s with exit code 1]
kluvin
  • 5,235
  • 3
  • 13
  • 20
  • https://stackoverflow.com/a/45299979/6665568 . This answer fully fixed the issue for me . Handles cases if u directly wanna replace if row already present. – Natesh bhat Mar 18 '19 at 13:05

2 Answers2

34

Change INSERT to INSERT OR IGNORE:

cur.execute('INSERT OR IGNORE INTO oldposts VALUES(?)', [submID])
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • +1 for my needs I very much prefer this than EAFP (easier to ask for forgiveness than permission) with python – lexc Feb 16 '22 at 13:17
10

Change "INSERT" to "INSERT OR REPLACE" it's better...

Update_Table = """INSERT OR REPLACE INTO station_statusDB VALUES(?, ?, ?, ?);"""
with sql.connect(station_statusDB) as Conn:
    print("Connected to:", station_statusDB)
    Conn.execute(New_Station_Table)
    while Client_Flag != 0:
        print("Updating Station Database...")
        Conn.execute(Update_Table, (ClientID, Client_date, Client_Alarm, Client_Water))
        print("Done!, Saving...")
        Conn.commit()
Sharon Zak
  • 101
  • 1
  • 2