From 6aa08bf4401427247bc82e43cc329d676681f2bf Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Fri, 8 Nov 2024 15:56:59 +0000 Subject: [PATCH] Add check_sqlite.py --- check_sqlite.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 check_sqlite.py diff --git a/check_sqlite.py b/check_sqlite.py new file mode 100644 index 0000000..1ee44ab --- /dev/null +++ b/check_sqlite.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import sqlite3 +import sys + +def main(db_path, query): + try: + sqliteConnection = sqlite3.connect(db_path) + cursor = sqliteConnection.cursor() + print("Database created and Successfully Connected to SQLite") + + cursor.execute(query) + + if 'select ' in query: + for record in cursor.fetchall(): + print(record) + else: + sqliteConnection.commit() + cursor.close() + + except sqlite3.Error as error: + print("Error while connecting to sqlite", error) + finally: + if sqliteConnection: + sqliteConnection.close() + print("The SQLite connection is closed") + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("ERROR: please pass the DB path and the query you want to execute.") + print("\nExamples:\n") + print(" python3 this_script.py /tmp/some_db_file \'select * from kv where key=\"charmers.openstack-release-version\";\'") + print(" python3 this_script.py /tmp/some_db_file \"\"\"select * from kv;\"\"\"") + print(" python3 this_script.py /tmp/some_db_file \"\"\"update kv set data='\\\"queens\\\"' where key='charmers.openstack-release-version';\"\"\"") + + else: + main(sys.argv[1], sys.argv[2])