"DatabaseError: database disk image is malformed" when syncing

Your media database may be corrupted if you receive the following error message with "media" mentioned on the second last line:

Syncing failed: 
Traceback (most recent call last): 
  File "aqt/sync.pyc", line 324, in run 
  File "aqt/sync.pyc", line 385, in _sync 
  File "aqt/sync.pyc", line 416, in _syncMedia 
  File "anki/sync.pyc", line 735, in sync 
  File "anki/media.pyc", line 336, in findChanges 
  File "anki/media.pyc", line 357, in _logChanges 
  File "anki/media.pyc", line 373, in _changes 
DatabaseError: database disk image is malformed

You can fix this issue without losing any data by having Anki recreate the file. To do so, close Anki and navigate to the User 1 folder in your Anki data folder:

https://docs.ankiweb.net/files.html#file-locations

Rename or remove collection.media.db2 while taking care not to touch the collection.media folder. When you start Anki and sync, it will recreate the file.

To minimize the chance of this happening again, please make sure you're running the latest Anki version.