17

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?

1
  • 1
    fwiw, increasing the timeout is likely only going to make this error less frequent. it's better to implement retries. Commented Feb 25, 2013 at 13:52

1 Answer 1

31

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})
Sign up to request clarification or add additional context in comments.

2 Comments

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}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.