use build.py instead of shell
authorgamesguru <mathmuncher11@gmail.com>
Tue, 9 Mar 2021 18:43:04 +0000 (13:43 -0500)
committergamesguru <mathmuncher11@gmail.com>
Tue, 9 Mar 2021 18:57:32 +0000 (13:57 -0500)
sql/build.py [new file with mode: 0755]
sql/build.sh [deleted file]
sql/data/TODO/sync_data.csv [moved from sql/data/sync_data.csv with 100% similarity]

diff --git a/sql/build.py b/sql/build.py
new file mode 100755 (executable)
index 0000000..a0673dd
--- /dev/null
@@ -0,0 +1,41 @@
+import csv
+import os
+import sqlite3
+
+SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
+os.chdir(SCRIPT_DIR)
+
+
+def main():
+    print("Cleanup...")
+    if os.path.isfile("nt.sqlite"):
+        os.remove("nt.sqlite")
+
+    print("\nPack nt.sqlite")
+    con = sqlite3.connect("nt.sqlite")
+    cur = con.cursor()
+    with open("tables.sql") as tables:
+        cur.executescript(tables.read())
+
+    for p in os.listdir("data"):
+        if not p.endswith(".csv"):
+            continue
+        t = os.path.splitext(os.path.basename(p))[0]
+        p = os.path.join("data", p)
+
+        # Loop over CSV files
+        with open(p) as f:
+            reader = csv.DictReader(f)
+            q = ",".join("?" * len(reader.fieldnames))
+            reader = csv.reader(f)
+            cur.executemany(f"INSERT INTO {t} VALUES ({q});", reader)
+
+    cur.close()
+    con.commit()
+    con.close()
+    print("\nDone!")
+    return True
+
+
+if __name__ == "__main__":
+    main()
diff --git a/sql/build.sh b/sql/build.sh
deleted file mode 100755 (executable)
index b0fc934..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/bash -e
-
-cd "$(dirname "$0")"
-
-rm_cmd="rm -f nt.sqlite"
-printf "\\n\e[1;31m${rm_cmd}\e[0m\\n\n"
-$rm_cmd
-
-pack_msg="==> Pack nt.sqlite"
-printf "\\n\\x1b[32m${pack_msg}\x1b[0m\n\n"
-
-# Create SQL file
-pack_cmd="sqlite3 nt.sqlite \".read init.sql\""
-# pack_cmd="sqlite3 nt.sqlite -init init.sql"
-printf "\\n\e[1;31m${pack_cmd}\e[0m\\n"
-bash -exec "$pack_cmd"
-# sqlite3 nt.sqlite