Search code examples
python-3.xjupyter-notebookgoogle-colaboratorygoogle-notebook

Error in accessing file uploaded file in google colab


I upload final_half.sqlite file in google colab. While reading the file it is giving me the error as below. Could anyone tell me how to solve this?

DatabaseError: database disk image is malformed

I uploaded the file in google colab as follows

import pandas as pd
import sqlite3
from google.colab import files
uploaded = files.upload()

I checked the status of the uploaded file

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

User uploaded file "final_half.sqlite" with length 7208960 bytes

for name, data in uploaded.items():
  with open('final_half.sqlite', 'wb') as f:
    f.write(data)
    print ('saved file', name)
    con = sqlite3.connect(name)
    print(con)
    sorted_data = pd.read_sql_query("""SELECT * FROM Reviews_half""", con)

Error:

DatabaseError                             Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1408             else:
-> 1409                 cur.execute(*args)
   1410             return cur

DatabaseError: database disk image is malformed

Solution

  • Change these lines

    for name, data in uploaded.items():
      with open('final_half.sqlite', 'wb') as f:
        f.write(data)
        print ('saved file', name)
        con = sqlite3.connect(name)
    

    To just

    con = sqlite3.connect('final_half.sqlite')
    

    The file is already saved, you don't need to write it again.