sqlite3.DatabaseError: database disk image is malformed

Hi Team Kolibri

I’m getting a new problem when starting Kolibri on the raspberry pi (see below):
This has happened just after I downloaded a lot of content on the pi.
Any idea of what to do?

kolibri start

INFO Running Kolibri with the following settings: kolibri.deployment.default.settings.base
Traceback (most recent call last):
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/backends/utils.py”, line 64, in execute
return self.cursor.execute(sql, params)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/backends/sqlite3/base.py”, line 323, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.DatabaseError: database disk image is malformed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “/usr/local/bin/kolibri”, line 11, in
sys.exit(main())
File “/usr/local/lib/python3.5/dist-packages/kolibri/utils/cli.py”, line 569, in main
initialize(debug=debug)
File “/usr/local/lib/python3.5/dist-packages/kolibri/utils/cli.py”, line 146, in initialize
django.setup()
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/init.py”, line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/apps/registry.py”, line 115, in populate
app_config.ready()
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/morango/apps.py”, line 27, in ready
InstanceIDModel.get_or_create_current_instance()
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/morango/models.py”, line 107, in get_or_create_current_instance
“database”: DatabaseIDModel.get_or_create_current_database_id(),
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/morango/models.py”, line 68, in get_or_create_current_database_id
return cls.objects.get(current=True)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/models/manager.py”, line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/models/query.py”, line 381, in get
num = len(clone)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/models/query.py”, line 240, in len
self._fetch_all()
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/models/query.py”, line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/models/query.py”, line 52, in iter
results = compiler.execute_sql()
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/models/sql/compiler.py”, line 848, in execute_sql
cursor.execute(sql, params)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/backends/utils.py”, line 64, in execute
return self.cursor.execute(sql, params)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/utils.py”, line 95, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/utils/six.py”, line 685, in reraise
raise value.with_traceback(tb)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/backends/utils.py”, line 64, in execute
return self.cursor.execute(sql, params)
File “/usr/local/lib/python3.5/dist-packages/kolibri/dist/django/db/backends/sqlite3/base.py”, line 323, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: database disk image is malformed

As a first step, could you try running kolibri manage migrate - sometimes initiating the migration (even though there are no migrations to apply) can help reset the database to a usable state.

Before you attempt these strategies, please make a copy of your db.sqlite3 file in your .kolibri folder!

If this does not work, there are two possible strategies. If there is no important user data, it may be simpler to delete the database, and setup the device again (the content would remain and would be reimported into the database).

Alternatively, it may be possible to recover the database data using an SQLite database dump and restore. This can be done with the following command:

$ sqlite3 db.sqlite3 ".dump" | sqlite3 new_db.sqlite3 from here: https://stackoverflow.com/a/18260642

Then copying the new database over the old database.

@richard it’s possible that Kolibri’s dbbackup and dbrestore commands can achieve this, because they do essentially dump data and load it back in. Worth investigating next time we come across a broken db.

Yes, excellent point!

Thanks @richard and @benjamin.

I erased db.sqlite3 and now it works.

Just a quick note to someone potentially reading the above, that while it will make the problem go away, it will also :warning: IRREVERSIBLY DELETE YOUR USER DATABASE :warning: