15

I am using sqlite (v2.6.0) as database backend and using sqlalchemy(v0.7.9) to operate it. Recently I got a error OperationalError: (OperationalError) database is locked

By searching stackoverflow a possible solution is to increase the timeout of a connection. Referece: OperationalError: database is locked

But I don't know how to did that in sqlalchemy (since connection are actually controlled by it) Can someone give me a direction?

Community
  • 1
  • 1
user1817188
  • 487
  • 1
  • 7
  • 14
  • 1
    fwiw, increasing the timeout is likely only going to make this error less frequent. it's better to implement retries. – andrew cooke Feb 25 '13 at 13:52

1 Answers1

30

SQLAlchemy's create_engine() takes an argument connect_args which is a dictionary that will be passed to connect() of the underlying DBAPI (see Custom DBAPI connect() arguments). sqlite3.connect() accepts timeout argument, so this should work:

create_engine('sqlite:///some.db', connect_args={'timeout': 15})
Audrius Kažukauskas
  • 13,173
  • 4
  • 53
  • 54
  • 13
    In case anyone is trying to solve the same problem for PostgreSQL 9.3, the connect_args argument would be spelled differently: connect_args={'connect_timeout': 15} – Steve Saporta Jun 11 '14 at 20:30
  • The actual argument name depends on the dialect and driver that is used to connect to the database. The @SteveSaporta comments relates most probably to the `psycopg2` dbapi, while PostgreSQL has other possible drivers such as `pg8000` which expects a `timeout` argument. – Sanandrea Sep 24 '19 at 14:36