-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to convert camera database to JSON file
- Loading branch information
1 parent
413f338
commit 9f65f5e
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os.path | ||
import sqlite3 | ||
import json | ||
|
||
def main(): | ||
|
||
# Create a connection to the database | ||
camera_db = sqlite3.connect("../cameras_db.db") | ||
cursor = camera_db.cursor() | ||
|
||
#get field names | ||
cursor.execute("PRAGMA table_info(cameras)") | ||
field_names = cursor.fetchall() | ||
|
||
cursor.execute("SELECT * FROM cameras") | ||
rows = cursor.fetchall() | ||
|
||
# Dump the data to a JSON file avoiding nulls | ||
with open("cameras.json", "w") as f: | ||
json.dump( | ||
[ | ||
{ | ||
field[1]: value | ||
for field, value in zip(field_names, row) | ||
if value is not None | ||
} | ||
for row in rows | ||
], | ||
f, | ||
indent=4, | ||
sort_keys=True, | ||
) | ||
cursor.close() | ||
camera_db.close() | ||
|
||
print(f"Data dumped to cameras.json, {len(rows)} rows.") | ||
|
||
if __name__ == '__main__': | ||
main() |