Chrome浏览器SQLite历史库报错sqlite3.OperationalError: database is locked求解决方案
Hey there! Let's tackle that sqlite3.OperationalError: database is locked issue with Chrome's history database. I've run into this exact problem a few times myself, so here are the most reliable fixes I've tested and used:
Shut down all Chrome processes completely
Chrome loves lingering in the background even after you close the main window—think incognito tabs, pinned apps, or sync processes. Open your task manager (Windows:Ctrl+Shift+Esc, macOS:Cmd+Opt+Esc), find every instance of Chrome, and end them all. Wait 10 seconds, then try accessing the database again.Work on a copy of the database file
Chrome stores its history DB at:- Windows:
%LOCALAPPDATA%\Google\Chrome\User Data\Default\History - macOS:
~/Library/Application Support/Google/Chrome/Default/History
Instead of modifying or reading the live file directly, make a full copy to another folder (like your desktop) and point your code to this copy. This eliminates conflicts with Chrome's active file locks entirely.
- Windows:
Check for other processes hogging the file
Third-party tools like backup software, antivirus scanners, or even other scripts you’ve run might be holding a lock on the History file. Uselsof(macOS/Linux) or Process Explorer (Windows) to identify which processes have the file open, then close those apps temporarily.Adjust your SQLite connection settings
If you’re only reading the history (the most common use case), open the database in read-only mode to avoid triggering write locks. In Python, that looks like:# Explicit read-only connection conn = sqlite3.connect('file:path/to/History?mode=ro', uri=True)You can also increase the timeout to give SQLite more time to wait for the lock to release:
conn = sqlite3.connect('path/to/History', timeout=30) # Waits 30 seconds instead of default 5Repair a potentially corrupted database
If the lock persists even after closing Chrome, the DB might be corrupted. Use the SQLite command-line tool to check and fix it:# First check integrity sqlite3 path/to/History "PRAGMA integrity_check;" # If errors are found, recover the database sqlite3 path/to/History ".recover" | sqlite3 path/to/History_fixedThen use the
History_fixedfile for your code.
One last tip: Chrome often rewrites the History file when it’s running, so the copy method is always the safest bet for read-only access—you won’t have to worry about sudden locks mid-operation.
内容的提问来源于stack exchange,提问作者zeroFighter

