import sqlite3
import os
import sys
def main(database_path):
query = 'SELECT name, data FROM tree;'
# Check if the SQLite database file exists
if not os.path.isfile(database_path):
print(f"Error: Database file '{database_path}' does not exist.")
print("Usage: python3 script.py <path_to_database>")
sys.exit(1)
# Connect to the SQLite database
conn = sqlite3.connect(database_path)
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# Execute the SQL query
cursor.execute(query)
# Fetch all rows from the database
rows = cursor.fetchall()
# Loop through the rows and write to files
for name, data in rows:
# Check if data is None and replace with an empty bytes object
if data is None:
data = b''
# Sanitize the name to create a valid filename
# Replace any forward slashes with underscores
filename = name.replace('/', '_')
# Write the data to the file in binary mode
with open(filename, 'wb') as file:
file.write(data)
# Print a message indicating the file has been written
print(f'Data written to file: {filename}')
# Close the cursor and connection
cursor.close()
conn.close()
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python3 script.py <path_to_database>")
sys.exit(1)
database_file = sys.argv[1]
main(database_file)