0

enter image description here

I am writing a code on replit and it works perfectly one the file I'm using. However since I want to try to do sometihng to the database that I'm not sure will work I wanted to copy the database onto another replit file, so I tried to download it and then re - upload it onto another database file, and then use this code:

import sqlite3
import os
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tabs = ttk.Notebook(root)
tabs.place(x=0, y=0)
tab1 = ttk.Frame(tabs)
tabs.add(tab1, text = "Workouts display")
tab2 = ttk.Frame(tabs)
tabs.add(tab2, text = "Checkboxes")
root.title("Gym")
root['background']='red'
add = 0
connection = sqlite3.connect("My database.txt")
c = connection.cursor()

dictionary = [["Leg Day",0],["Shoulder Day",1],["Arm Day",2],["Back Day",3]]
scrollbar = tk.Scrollbar(tab1)
myList = tk.Listbox(tab2, yscrollcommand=scrollbar.set)

c.execute('''
SELECT * FROM Workout
''')
root.mainloop()

to try and call this new database that I've uploaded on this "backup" file so that I can refer to if anything goes wrong in my original. However it's saying that the database disk image is malformed. I've tried several times and it's returning the same message: "sqlite3.DatabaseError: database disk image is malformed"

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • 1
    Its a bit odd that your database is a ".txt" file (`my database.txt`). It may not really be a sqlite database. – topsail May 19 '23 at 15:06
  • Yea it for some reason downloads as a txt file. I tried to dowload it as an "All files" option too as one of my tries but that doesn't work either. My computer doesn't give me any other options of dowloading. Basically I don't know how to download or export sqlite3 database files while retaining their actual original form. – Armaan Khaitan May 19 '23 at 15:08
  • Btw it wasn't originally. It for some reason made me download it as a txt file – Armaan Khaitan May 19 '23 at 15:09
  • I added a picture as a hyper link on how my computer offers to download – Armaan Khaitan May 19 '23 at 15:11
  • Do look inside the file to see what it is. It could be mangled by CRLF encoding or who knows what. How can we reproduce this problem? – tadman May 19 '23 at 15:11
  • Another option? [How to export sqlite to CSV in Python without being formatted as a list?](https://stackoverflow.com/q/10522830) – 001 May 19 '23 at 15:17
  • Have you perhaps considered opening that text file with a text viewer/editor? – tevemadar May 19 '23 at 15:18
  • You can also try to open you sqlite file with a sqlite viewer - if that works at least you know the db is okay (which I guess probably it isn't and probably you won't be able to open it with a sqlite viewer either) – topsail May 19 '23 at 15:40

1 Answers1

0

(Workaround at the end)

You see a combination of 2 things, I can replicate it too:

  1. Your database file has no extension, and for some reason the browser slaps a .txt extension to it. This is not a problem, while it's not a real text file, it could work
  2. If you click on the file on replit, you get to see it in a new tab, lots of red NULL-s. For whatever reason viewing the file affects how you download it (even if you close the tab with this strange content).

Created this fascinating test code, based on the tutorial:

import sqlite3

con = sqlite3.connect("tutorial")
cur = con.cursor()

cur.execute("CREATE TABLE movie(title, year, score)")
cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
con.commit()

res = cur.execute("SELECT score FROM movie")
print(res.fetchall())

Running in place it produces the desired [(8.2,), (7.5,)] output, and creates the database file tutorial.
Now DON'T click on the filename, but click on the vertical ... next to it, and select download. As a result you will get a 8192-byte long tutorial.txt. Which you can connect to, and running the same code locally, with modified filename and removed CREATE/INSERT/commit() lines:

import sqlite3

con = sqlite3.connect("tutorial.txt")
cur = con.cursor()

res = cur.execute("SELECT score FROM movie")
print(res.fetchall())

still produces [(8.2,), (7.5,)].

Now click on the filename, tutorial, and see the lots of red NULL-s: When you see this, the database can't be downloaded properly. If you click on download now, you will get a 8206-byte long non-working tutorial.txt, which then dies on the connect() call. The file got longer, but not too much longer, which probably means that line-breaks are converted to 2-byte carriage-return + line-break sequences, what @tadman guesses in a comment.

However this is a UI bug, replit keeps the destroyed file on the client side, and will provide it for you to download. But the original file on the server still works, you just have to get rid of the one in your browser:

  1. Make sure that the database file with its strange view is not open (inside the replit window/tab)
  2. Do a clean reload of the replit window/tab (Ctrl+F5 or something like that)
  3. Download the database file directly via the ... menu, carefully avoiding to actually view the file.
tevemadar
  • 12,389
  • 3
  • 21
  • 49