From: gamesguru Date: Tue, 9 Mar 2021 18:43:04 +0000 (-0500) Subject: use build.py instead of shell X-Git-Tag: 0.0.0~1 X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=9c8da2fb5ac7b9636f71302f655902881a6ffb58;p=nutratech%2Fnt-sqlite.git use build.py instead of shell --- diff --git a/sql/build.py b/sql/build.py new file mode 100755 index 0000000..a0673dd --- /dev/null +++ b/sql/build.py @@ -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 index b0fc934..0000000 --- a/sql/build.sh +++ /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 diff --git a/sql/data/sync_data.csv b/sql/data/TODO/sync_data.csv similarity index 100% rename from sql/data/sync_data.csv rename to sql/data/TODO/sync_data.csv