Skip to content

Commit

Permalink
Add script to convert camera database to JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
imigueldiaz committed Apr 20, 2024
1 parent 413f338 commit 9f65f5e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/cameras_db/setup/cameras_to_json.py
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()

0 comments on commit 9f65f5e

Please sign in to comment.