-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_helper.py
131 lines (117 loc) · 3.75 KB
/
sql_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sqlite3
import numpy as np
def connect(db_file):
try:
return sqlite3.connect(db_file)
except sqlite3.Error as e:
print(e)
columns = ['timestamp','frame','x','y','w','h','p','angle','long_axis','short_axis','radial_position','measured_velocity','cell_id',
'stress','strain','area','pressure','vel_fit_error','vel','vel_grad','eta','eta0','delta','tau','omega','Gp1','Gp2','k','alpha']
def create_table(db_file):
try:
conn = connect(db_file)
c = conn.cursor()
sql_create_data_table = "CREATE TABLE IF NOT EXISTS data ("
for var in columns:
sql_create_data_table += f'{var} real,'
sql_create_data_table += ' UNIQUE(timestamp,x,y,w,h,p))'
print(sql_create_data_table)
c.execute(sql_create_data_table)
conn.commit()
conn.close()
except sqlite3.Error as e:
print(e)
def write_ellipses(db_file,entries):
#entries (x,6)
print('ENTRIES.SHAPE',entries.shape)
try:
conn = connect(db_file)
c = conn.cursor()
c.executemany("INSERT INTO data(x,y,w,h,p,frame,timestamp) VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING", entries)
conn.commit()
conn.close()
except sqlite3.Error as e:
print(e)
def write_mechanical(db_file,df):
sql_question_marks = ""
sql_add_mechanical = "INSERT OR REPLACE INTO data("
for var in columns:
sql_add_mechanical += f'{var},'
sql_question_marks += '?,'
sql_add_mechanical = sql_add_mechanical[:-1] + ') VALUES (' + sql_question_marks[:-1] + ')'#ON CONFLICT DO REPLACE'#REPLACE'
print(sql_add_mechanical)
entries = df[columns].to_numpy()
try:
conn = connect(db_file)
c = conn.cursor()
c.executemany(sql_add_mechanical,entries)
conn.commit()
conn.close()
except sqlite3.Error as e:
print(e)
def fetch_all(db_file):
try:
conn = connect(db_file)
c = conn.cursor()
c.execute("SELECT * FROM data")
print(c.fetchall())
conn.commit()
conn.close()
except sqlite3.Error as e:
print(e)
def fetch_ellipses_larger_t0(db_file, t0):
try:
conn = connect(db_file)
c = conn.cursor()
c.execute("SELECT timestamp,frame,x,y,w,h,p FROM data WHERE timestamp > (?);",(t0,))
items = c.fetchall()
conn.commit()
conn.close()
return items
except sqlite3.Error as e:
print(e)
def fetch_larger_t0(db_file, t0):
try:
conn = connect(db_file)
c = conn.cursor()
c.execute("SELECT * FROM data WHERE timestamp > (?);",(t0,))
items = c.fetchall()
conn.commit()
conn.close()
return items
except sqlite3.Error as e:
print(e)
def clear_db(db_file):
try:
conn = connect(db_file)
c = conn.cursor()
c.execute("DROP TABLE data")
conn.commit()
conn.close()
except sqlite3.Error as e:
print(e)
# NOT working, unknown reason
def fetch_larger_t0_json(db_file, t0):
try:
conn = connect(db_file)
c = conn.cursor()
c.execute("SELECT json_object(*) FROM data WHERE timestamp > (?);",(t0,))
items = c.fetchall()
conn.commit()
conn.close()
return items
except sqlite3.Error as e:
print(e)
#working
def fetch_last_3_ellipses(db_file):
try:
conn = connect(db_file)
c = conn.cursor()
# c.execute("SELECT json_object(*) FROM data WHERE timestamp > (?);",(t0,))
c.execute("SELECT timestamp,frame,x,y,w,h,p FROM data ORDER BY timestamp DESC LIMIT 3;")
items = c.fetchall()
conn.commit()
conn.close()
return items
except sqlite3.Error as e:
print(e)