13

I'm trying to insert all values of a list to my sqlite3 database. When I simulate this query by using the python interactive interpreter, I am able to insert the single value to DB properly. But my code fails while using an iteration:

...
connection=lite.connect(db_name)
cursor=connection.cursor()
for name in match:
         cursor.execute("""INSERT INTO video_dizi(name) VALUES (?)""",(name,))
connection.commit()
...

error:cursor.execute("""INSERT INTO video_dizi(name) VALUES (?)""",(name,))
sqlite3.OperationalError: database is locked

Any way to overcome this problem?

Fish
  • 135
  • 1
  • 1
  • 4
  • 1
    For a good description of this error see this answer: http://stackoverflow.com/a/26864360/1157720 – sajjadG Feb 01 '16 at 08:00
  • Does this answer your question? [operational error: database is locked](https://stackoverflow.com/questions/26862809/operational-error-database-is-locked) – sebisnow Aug 18 '20 at 07:59

3 Answers3

14

Do you have another connection elsewhere in your code that you use to begin a transaction that is still active (not committed) when you try to commit the operation that fails?

hasseg
  • 6,787
  • 37
  • 41
  • Only code related with sqlite3: import sqlite3 as lite db_name="diziport.sqlite" – Fish Apr 03 '11 at 13:24
  • 3
    Sir, I'm really sorry but it is working now after closing interactive interpreter (in active mode) – Fish Apr 03 '11 at 13:27
  • @hasseg it would be nice to know how to fix it without having to restart Python. I have tried cursor.close() and connection.close(), del(cursor), del(connection). Still get this error. – eric Oct 29 '14 at 02:10
  • Note python's sqlite3 module by default gives you a connection where you have to commit every change. This means an uncommitted change is easy to do. If you want "autocommit mode" (no explicit commits required), connect with `isolation_level=None`. – dfrankow Jan 25 '22 at 22:17
1

As this error can happen because you have opened your site.db or database file in DBbrowser type application to view in interactive database interface. Just close that it will work fine.

Fronto
  • 374
  • 2
  • 12
0

Because your database is use by another process or connection. If you need real concurrency, use a real RDBMS.

  • 1
    Not necessarily true. Berkeley DB's SQL API supports both the easy-to-use SQLite API as well as concurrent read-write operations. You can read about it here: http://bit.ly/hY6MTm. – dsegleau Apr 05 '11 at 02:50
  • 1
    Sqlite can support better concurrency by turning on WAL mode and increasing timeouts. – Erik Aronesty Nov 07 '18 at 16:20