From b33dce5edcd3d20a6686a719a84f0de8c1b4b412 Mon Sep 17 00:00:00 2001
From: Scott W Harden <swharden@gmail.com>
Date: Thu, 1 Mar 2018 20:06:56 -0500
Subject: [PATCH] sqlite demo

---
 sql/demo/create.py    |  63 ++++++++++++++++++++++++++++++++++--------
 sql/demo/surgeries.db | Bin 24576 -> 24576 bytes
 2 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/sql/demo/create.py b/sql/demo/create.py
index a5112c1..07a71c1 100644
--- a/sql/demo/create.py
+++ b/sql/demo/create.py
@@ -13,10 +13,15 @@
        VALUES (12,'',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
 
 """
+
+
+
 import collections
+import os
+import sqlite3
 
-if __name__=="__main__":
-    with open (R"X:\Data\surgeries\surgery_log.csv") as f:
+def csvToTable(fname="surgery_log.csv"):
+    with open (os.path.join(fname)) as f:
         raw=f.read().split("\n")
     rows=[]
     for line in raw:
@@ -36,18 +41,54 @@
         fields["dateSac"]=line[10]
         fields["notes"]=line[12]
         fields["hidden"]=0
-        rows.append(fields)
         keys=list(fields.keys())
         values=list(fields.values())
         for i,val in enumerate(values):
             if type(val)==int:
-                val=str(val)
-            elif len(val)==0:
-                val='NULL'
+                values[i]=str(val)
+            elif len(val.strip())==0:
+                values[i]='NULL'
             else:
-                val="`%s`"%val
-            print(val)
-        #sqlIns="INSERT INTO `animals` ()"
-        #print(sql)
-        print()
+                values[i]="`%s`"%val
+        rows.append([keys,values])
+    return rows
+
+def sqlInsertRow(row):
+    sqlKey,sqlVal=[],[]
+    for i in range(len(row[0])):
+        key,val=row[0][i],row[1][i]
+        sqlKey.append("`%s`"%key)
+        sqlVal.append(str(val))
+    sql="INSERT INTO `animals` (%s) \nVALUES (%s);"%(", ".join(sqlKey),", ".join(sqlVal))
+    return sql
+
+if __name__=="__main__":
+    # load data from the CSV file
+    rows=csvToTable()
+
+    # create an empty database    
+    conn = sqlite3.connect(r"surgeries.db")
+    c = conn.cursor()
+    c.execute("DROP TABLE `animals`") # delete the old data
+
+    # create the database structure
+    sql="""
+        CREATE TABLE "animals" ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
+                                `animal` TEXT NOT NULL, `originalCage` TEXT, `gender` NUMERIC,
+                                `genotype` NUMERIC, `target` TEXT, `substance` TEXT, `volume` TEXT,
+                                `coords` TEXT, `dateSx` TEXT, `dateSac` TEXT, `notes` TEXT, `hidden` INTEGER );
+        """
+    c.execute(sql.strip())
+
+    # copy CSV data into the new database
+    for row in rows:
+        sql=sqlInsertRow(row)
+        sql=sql.replace("`","'")
+        print(sql)
+        conn.execute(sql)
     print("DONE")
+
+    # save database to disk
+    conn.commit()
+    conn.close()
+    print("DONE")
\ No newline at end of file
diff --git a/sql/demo/surgeries.db b/sql/demo/surgeries.db
index 7dc2b8d00b2b15f09bfc90bf06b674704eb93e1e..8a45988465a557bc50fd032bb6c3fcdf994415ee 100644
GIT binary patch
literal 24576
zcmeI3U2Gd!701W1o!Cz7+cb4k*PZ%0Rz;)Qllid6<09BPS#L;dC#mB!v_(qXc#_P@
zv3DJ(?d}uO!ang-ApsKbQl1tbc3*hn0fB@T3859@0r7zL0fB^&cEuvZ!y<9ceA!8z
z4?ZMXbVr&qp7G58p7T5B-npKUS8kV$u1>ewo!xquD%=^)<Kb>l%5mH*ti!N+VI6{X
z8CD;x&f;-C9J8I}1kb&2<QtrK?CDTsZtN-fdH93y2gh8&zG4Iz0Y-okU<4QeMt~7u
z1Q>zmlE7Qz=SC*i);wSJbn6eA`rhkJ_$l$A(|)w4ch)<4tG=tRZ?>EJyRG$>{%HO5
zC;8{9rDCl_YsJ}eiN3}AMkeEN&)egsLcL||)|-3wX6V4FQ})p*dX;V%jSaf6SS!t!
zs&sLwMi*DhWx8Bls1&Pr>8;XTT3oFyEi8gSrL<V1tBVV_S4-&-Jz?3f+Ss7A(icJ0
z7EaTRcE{K@TJ`2!eOtG_g@fCAtD$!`pz%tnx-fSNdAOq8ed9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