# sqlite3.DatabaseError: database disk image is malformed

**URL:** <https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507>\
**Category:** Kolibri\
**Created:** [February 22, 2018, 3:05pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507 "2018-02-22T15:05:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![clementine](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@clementine](https://community.learningequality.org/u/clementine)\
**Post date:** [February 22, 2018, 3:05pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507/1 "2018-02-22T15:05:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.learningequality.org/richard/32/10_2.png) [@richard](https://community.learningequality.org/u/richard)\
**Post date:** [February 22, 2018, 4:55pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507/2 "2018-02-22T16:55:31Z")

</div>

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](https://stackoverflow.com/a/18260642)

Then copying the new database over the old database.

---

<div class="post-metadata">

**Author:** ![benjamin](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.learningequality.org/benjamin/32/574_2.png) [@benjamin](https://community.learningequality.org/u/benjamin)\
**Post date:** [February 22, 2018, 8:11pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507/3 "2018-02-22T20:11:53Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.learningequality.org/richard/32/10_2.png) [@richard](https://community.learningequality.org/u/richard)\
**Post date:** [February 22, 2018, 8:19pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507/4 "2018-02-22T20:19:22Z")

</div>

Yes, excellent point!

---

<div class="post-metadata">

**Author:** ![clementine](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@clementine](https://community.learningequality.org/u/clementine)\
**Post date:** [February 23, 2018, 12:29pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507/5 "2018-02-23T12:29:46Z")

</div>

Thanks @richard and @benjamin.

I erased db.sqlite3 and now it works.

---

<div class="post-metadata">

**Author:** ![benjamin](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.learningequality.org/benjamin/32/574_2.png) [@benjamin](https://community.learningequality.org/u/benjamin)\
**Post date:** [February 24, 2018, 10:48pm UTC](https://community.learningequality.org/t/sqlite3-databaseerror-database-disk-image-is-malformed/507/6 "2018-02-24T22:48:21Z")

</div>

> [@clementine](#):
>
> 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 ⚠ **IRREVERSIBLY DELETE YOUR USER DATABASE** ⚠
