How do I unlock a SQLite database?

前端 未结 30 1397
栀梦
栀梦 2020-11-22 15:56
sqlite> DELETE FROM mails WHERE (`id` = 71);
SQL error: database is locked

How do I unlock the database so this will work?

相关标签:
30条回答
  • 2020-11-22 16:32

    One common reason for getting this exception is when you are trying to do a write operation while still holding resources for a read operation. For example, if you SELECT from a table, and then try to UPDATE something you've selected without closing your ResultSet first.

    0 讨论(0)
  • 2020-11-22 16:33

    Should be a database's internal problem...
    For me it has been manifested after trying to browse database with "SQLite manager"...
    So, if you can't find another process connect to database and you just can't fix it, just try this radical solution:

    1. Provide to export your tables (You can use "SQLite manager" on Firefox)
    2. If the migration alter your database scheme delete the last failed migration
    3. Rename your "database.sqlite" file
    4. Execute "rake db:migrate" to make a new working database
    5. Provide to give the right permissions to database for table's importing
    6. Import your backed up tables
    7. Write the new migration
    8. Execute it with "rake db:migrate"
    0 讨论(0)
  • 2020-11-22 16:35

    I had this problem just now, using an SQLite database on a remote server, stored on an NFS mount. SQLite was unable to obtain a lock after the remote shell session I used had crashed while the database was open.

    The recipes for recovery suggested above did not work for me (including the idea to first move and then copy the database back). But after copying it to a non-NFS system, the database became usable and not data appears to have been lost.

    0 讨论(0)
  • 2020-11-22 16:35

    From your previous comments you said a -journal file was present.

    This could mean that you have opened and (EXCLUSIVE?) transaction and have not yet committed the data. Did your program or some other process leave the -journal behind??

    Restarting the sqlite process will look at the journal file and clean up any uncommitted actions and remove the -journal file.

    0 讨论(0)
  • 2020-11-22 16:37

    I caused my sqlite db to become locked by crashing an app during a write. Here is how i fixed it:

    echo ".dump" | sqlite old.db | sqlite new.db
    

    Taken from: http://random.kakaopor.hu/how-to-repair-an-sqlite-database

    0 讨论(0)
  • 2020-11-22 16:38

    If you want to remove a "database is locked" error then follow these steps:

    1. Copy your database file to some other location.
    2. Replace the database with the copied database. This will dereference all processes which were accessing your database file.
    0 讨论(0)
提交回复
热议问题