Skip to content

Instantly share code, notes, and snippets.

@t5k6
Created December 9, 2024 20:40
Show Gist options
  • Select an option

  • Save t5k6/3072c455f72d4fc1dadfc94469977f2d to your computer and use it in GitHub Desktop.

Select an option

Save t5k6/3072c455f72d4fc1dadfc94469977f2d to your computer and use it in GitHub Desktop.
How to Solve Calibre Issues with Kobo Database Corruption

Problem

When attempting to connect a Kobo device to Calibre, the following error may occur:

ERROR: Error: Error communicating with device

CorruptError: database disk image is malformed

Traceback (most recent call last):
  File "calibre\gui2\device.py", line 112, in run
  File "calibre\gui2\device.py", line 566, in _books
  File "calibre\devices\kobo\driver.py", line 2091, in books
  File "C:\t\t\apsw-42s16m2x\src\cursor.c", line 240, in resetcursor
apsw.CorruptError: CorruptError: database disk image is malformed

This occurs when the KoboReader.sqlite database is corrupted or contains invalid entries.


Solution

  1. Open the Database in SQLite Tool:

    • Use SQLiteBrowser or another SQLite tool to open the KoboReader.sqlite database.
  2. Export the Database to SQL:

    • Use the tool's export functionality:
      File > Export > Database to SQL file
      
    • This creates a plain-text SQL file that includes the database schema and data.
  3. Inspect and Edit the SQL File:

    • Open the exported SQL file in a text editor.
    • Look for invalid entries or table definitions. The issue I've encountered was invalid default values, such as:
      "WishlistedDate" TEXT DEFAULT 0000-00-00T00:00:00.000,
      "DateModified" TEXT DEFAULT 0000-00-00T00:00:00.000,
    • These values are invalid in SQLite because they lack quotes around the default date values.
  4. Fix the SQL File:

    • Correct invalid lines by wrapping default values in quotes:
      "WishlistedDate" TEXT DEFAULT '0000-00-00T00:00:00.000',
      "DateModified" TEXT DEFAULT '0000-00-00T00:00:00.000',
  5. Reimport the SQL File:

    • In the SQLite tool, create a new database and import the corrected SQL file:
      File > Import > Database from SQL file
      
  6. Replace the Corrupted Database:

    • Replace the original KoboReader.sqlite file on the device with the repaired version.
  7. Verify and Test:

    • Run PRAGMA integrity_check; on the new database to confirm it is valid.
    • Reconnect the Kobo to Calibre and ensure the device is detected correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment