Database corrupt? What to do?

I have been redesigning my Chart of Accounts. Added new accounts, moved existing accounts to new groups, regrouped, recoded some transactions to other accounts. During the process, from time to time, I had to undo some actions.

Now my database seems corrupted. I have a script that is supposed to retrieve the latest transactions through the api. It used to work. But now it doesn’t anymore. Sometimes, it will retrieve transactions from the past instead of the latest ones. At other times, it will retrieve payments that I have deleted (and, indeed, don’t show up anymore in the Payments tab).

For the record, this is the url I use to retrieve the latest payments:

http://localhost:55667/api2/payments?sortByDesc=true&sortBy=Date&skip=0&pageSize=100

I don’t see anything wrong with it, especially since it still works correctly on other databases. So I think there is something wrong with my current database, probably as a result of my redesign work.

Which leads me to the following questions:

  • Is there a tool to check the database for corruption?
  • Is there a tool to fix a corrupted database (within reason)?

Thanks for any insights!

1 Like

This worked for me perfectly:

:one: Make sure Python is installed

Open a terminal / command prompt and run:

python --version

or

python3 --version

If it shows a version (3.10+), you’re good.
If not, install Python from https://python.org.


:two: Save the Script to a .py file

Open any text editor (VS Code, Notepad++, PyCharm, etc.)
Paste the code from above into a new file and save it, for example as:

repair_manager.py

Make sure it’s in the same folder as your manager.manager file, or adjust the file path accordingly.


:three: Put the Database File in the Same Folder

Move your manager.manager file into the same directory as repair_manager.py (or edit the path in the code to point to the real location of your DB file).

Example folder layout:

C:\Users\YourName\Projects\repair_db\
 ├─ manager.manager
 ├─ repair_manager.py


:four: Open a Terminal in that Folder

  • Windows: Shift + Right Click in the folder → “Open PowerShell window here” or “Open Terminal here.”

  • macOS/Linux: Use cd in Terminal to navigate to the folder:

    cd /path/to/repair_db
    
    

:five: Run the Script

python repair_manager.py

or

python3 repair_manager.py


:six: Check the Output

If successful, you’ll get:

Dump created successfully: manager_dump.sql
New repaired database created successfully: manager_fixed.manager

Then you can run the second short Python script (integrity check) in the same way — save it as check_integrity.py or run it in an interactive Python shell.


:seven: Optional: Use VS Code’s “Run” Button

Since you’ve already installed Visual Studio Code (per your memory), you can:

  • Open VS Code.

  • File → Open Folder → choose the folder containing the script.

  • Open the repair_manager.py file.

  • Click the green “Run Python File” button at the top-right.

    Code:

    import sqlite3
    import os

Paths

old_db_path = “manager.manager” # original DB file
new_db_path = “manager_fixed.manager” # repaired DB file
dump_file = “manager_dump.sql” # temporary dump file

print(“=== Step 1: Dumping the old database ===”)
try:
conn = sqlite3.connect(f"file:{old_db_path}?mode=ro", uri=True)
with open(dump_file, “w”, encoding=“utf-8”) as f:
for line in conn.iterdump():
f.write(f"{line}\n")
conn.close()
print(“Dump created successfully:”, dump_file)
except sqlite3.DatabaseError as e:
print(“Error dumping database:”, e)
exit(1)

print(“\n=== Step 2: Creating a new repaired database ===”)
try:
if os.path.exists(new_db_path):
os.remove(new_db_path)
new_conn = sqlite3.connect(new_db_path)
with open(dump_file, “r”, encoding=“utf-8”) as f:
new_conn.executescript(f.read())
new_conn.commit()
new_conn.close()
print(“New repaired database created successfully:”, new_db_path)
except sqlite3.DatabaseError as e:
print(“Error rebuilding database:”, e)
exit(1)

print(“\n=== Step 3: Integrity check on the new database ===”)
try:
conn = sqlite3.connect(new_db_path)
res = conn.execute(“PRAGMA integrity_check;”).fetchone()
conn.close()
print(“Integrity check result:”, res)
if res and res[0] == “ok”:
print(“\n✅ Your repaired database passed the integrity check.”)
else:
print(“\n⚠️ Integrity check found issues:”, res)
except sqlite3.DatabaseError as e:
print(“Error running integrity check:”, e)

print(“\nAll steps finished.”)

1 Like

Thank you @Yaser for your reply.

I ran your script from VSC and it finished without error. Unfortunately, it does not seem to work for me as the generated manager_fixed.manager file, though it can be opened by Manager, shows no transactions.

I guess my database is beyond repair. I still wonder what caused this corruption.

Yet, you showed me the way how to access the database within Manager. I had been wondering how to do that. I might be able to use it in the future. Thank you for that.

Thanks again for your trouble. Best regards.

1 Like