相关文章推荐
耍酷的木瓜  ·  mysql ...·  5 天前    · 
完美的稀饭  ·  Microsoft (R) Inbox ...·  10 月前    · 
年轻有为的滑板  ·  operator keyword - ...·  11 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Why do I get a "database is locked" error on this simple python sqlite3 update query code?

Ask Question

Every SQLITE query I make usually goes fine, except I cannot seem to be able to update the DB.

db = sqlite3.connect("data.db")
cursor = db.cursor()
str_datetime = str(datetime.datetime.now())
cursor.execute("""UPDATE ads SET publish_end_datetime = ? WHERE ad_code = ?""", (str_datetime, 1411671200))
db.commit() # is this commit statement even necessary?
db.close()

I get the following operational error:

OperationalError Traceback (most recent call last) in () 3 str_datetime = str(datetime.datetime.now()) ----> 5 cursor.execute("""UPDATE ads SET publish_end_datetime = ? WHERE ad_code = ?""", (str_datetime, 1411671200)) 6 db.commit() 7 db.close()

OperationalError: database is locked

Please make sure no other program is working on this database, for example, if you are using pycharm, please break pycharm database connection first. – Yang HG Mar 20, 2019 at 2:45 Most likely your database is being used elsewhere or perhaps a previous connection is still open.2 things can't happen at once, that is update AND read cannot happen or update AND write so maybe check if you're closing you connections wherever you've opened them previously and try to re-run the query. – Zer0 Mar 20, 2019 at 2:45 timeout=20 didn't work, no other program should be working on the DB. I use DB Browser (SQLite), but it's closed. I program python in Jupyter Notebooks, and prior to running the code above, i spam a cell containing db.close() I really don't see where concurrence could be coming from... Weirder, if I replace the update statement by a select statement, in the same cell (block of code), the query works... – Vincent Labrecque Mar 20, 2019 at 2:54 OneDrive was likely the problem. I copied my db file on desktop and the update statement worked. – Vincent Labrecque Mar 20, 2019 at 3:12

I think I've found the problem. Whenever the DB is in use, a database.db-journal file is created in the same location. That's the main indicator that the DB is in use. I have not found what was "using" my DB, but I've found how to solve my problem. It appears that OneDrive might have been the problem. I copied my data.db on the desktop, outisde of OneDrive, and I finally have been able to get the update statement to work.

I hope it helps other people.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.