-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_sqlite_data.py
More file actions
43 lines (36 loc) · 1.35 KB
/
export_sqlite_data.py
File metadata and controls
43 lines (36 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# export_sqlite_data.py
import sqlite3
import json
import os
from datetime import datetime
# Function to convert datetime objects to strings
def serialize_datetime(obj):
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError(f"Type {type(obj)} not serializable")
# Connect to SQLite database - use your actual database path
db_path = r"E:\Sync Lab\Automate-Real-Estate-Field\instance\real_estate.db"
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
def get_table_data(table_name):
"""Get all data from a table as a list of dictionaries"""
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
return [dict(row) for row in rows]
# Get the list of tables
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [table[0] for table in cursor.fetchall()]
# Export data from each table
data = {}
for table in tables:
print(f"Exporting table: {table}")
data[table] = get_table_data(table)
print(f" - Exported {len(data[table])} rows")
# Save data to JSON file
export_file = 'sqlite_export.json'
with open(export_file, 'w', encoding='utf-8') as f:
json.dump(data, f, default=serialize_datetime, indent=2, ensure_ascii=False)
print(f"Data exported to {export_file}")
conn.close()