Skip to content

Commit

Permalink
sqlite demo
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Mar 2, 2018
1 parent 7cc4deb commit b33dce5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions sql/demo/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Binary file modified sql/demo/surgeries.db
Binary file not shown.

0 comments on commit b33dce5

Please sign in to comment.