Search code examples
pythonsqlalchemypyramidpylons

Issues with db in pyramid + Sqlalchemy


I am creating a simple app in pyramid . While inserting data i am getting db locked error.

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked [SQL: 'INSERT INTO users (name, email, number) VALUES (?, ?, ?)'] [parameters: ('test', '[email protected]', '123654')]

But first time it inserts the data correctly and on 2nd time it gives this error.

Any idea why this is happening second time ?

Here is my code :

name = request.params['name']
email = request.params['email']
no = request.params['number']
DBSession.add(User(name, email, no))

# Get the new ID and redirect
users = DBSession.query(User).all()

Solution

  • SQLite can only handle 1 concurrent transaction.

    Have you tried commit()before performing a query() then close() to end the session?

    name = request.params['name']
    email = request.params['email']
    no = request.params['number']
    DBSession.add(User(name, email, no))
    
    # Commit the transaction if complete
    DBSession.commit()
    
    # Get the new ID and redirect
    users = DBSession.query(User).all()
    
    # Close the session
    DBSession.close()