From 50a548fcc2519d78a8d4d0816dafda6cea297c3e Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Fri, 2 Jul 2021 09:36:22 +0300 Subject: [PATCH] add: database existence check --- util/import_photos.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/util/import_photos.py b/util/import_photos.py index 231e45c..fdee042 100644 --- a/util/import_photos.py +++ b/util/import_photos.py @@ -6,6 +6,7 @@ import filetype from os import path, walk from sys import argv, stderr from shutil import move +import sqlite3 # update database residing here DB_LOCATION = "photovoter.dblite" @@ -69,17 +70,43 @@ def update_database(): or create a new one, if it does not exist yet """ # make sure the database exists + check_database(DB_LOCATION) # insert new pictures to the image table pass -def check_database(): +def check_database(database_path: str): """Check if there is a database at DB_LOCATION. Just return if there is. If not, create a new one. """ # db exists? + if path.exists(database_path): + print("DB exists, updating", database_path) + return # make one - pass + else: + print("No DB, creating", database_path) + + con = sqlite3.connect(database_path) + cur = con.cursor() + # Create table + cur.execute( + """CREATE TABLE images + ( + imgid INTEGER PRIMARY KEY, + resizedpath TEXT NOT NULL, + origpath TEXT NOT NULL, + date TEXT, + GPSLatitude TEXT, + GPSLatitudeRef TEXT, + GPSLongitude TEXT, + GPSLongitudeRef TEXT + )""" + ) + con.commit() + # we only use this occasionaly with the new databases, + # so don't bother with transfer logic, just close and reopen it later + con.close() def main(): @@ -89,6 +116,7 @@ def main(): # process each pic and add it to the database for pic in process_pictures(): + update_database() print(pic) # just print into strout for now