Created
July 23, 2019 09:05
-
-
Save optimistanoop/1c04a32f248b5c6d893240e5c151bf0c to your computer and use it in GitHub Desktop.
Offline sqlite python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
class Offline: | |
def __init__(self): | |
print('hello offline') | |
self.conn = sqlite3.connect('offline.db') | |
c = self.conn.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS visitors (id INTEGER PRIMARY KEY, clientId text, locationId text, cameraNumber text, frame text)''') | |
c.execute('''CREATE TABLE IF NOT EXISTS errors (id INTEGER PRIMARY KEY, clientId text, locationId text, cameraNumber text, frame text, errors text, er_cat text)''') | |
# sql1 = 'DELETE FROM Frames_and_Scores' | |
# c.execute(sql1) | |
# conn.commit() | |
self.conn.commit() | |
self.conn.close() | |
print('tables created') | |
def storeInSQL(self, data): | |
print('storeInSQL') | |
if self.getCount() < 200: | |
self.conn = sqlite3.connect('offline.db') | |
c = self.conn.cursor() | |
# if err then push to err otherwise push to visitors | |
if 'erros' in data: | |
c.execute("INSERT INTO visitors(clientId, locationId, cameraNumber, frame) VALUES (?,?,?,?)",(data['clientId'], data['locationId'], data['cameraNumber'], data['frame'])) | |
else: | |
c.execute("INSERT INTO errors(clientId, locationId, cameraNumber, frame, errors, er_cat) VALUES (?,?,?,?.?,?)",(data["clientId"], data["locationId"], data["cameraNumber"], data["frame"], data['errors'], data['er_cat'])) | |
self.conn.commit() | |
self.conn.close() | |
self.isOfflineDataStored = True | |
print('data stored in sql') | |
def getCount(self): | |
self.conn = sqlite3.connect('offline.db') | |
c = self.conn.cursor() | |
sql = 'SELECT count(*) FROM visitors' | |
c.execute(sql) | |
count = c.fetchone() | |
self.conn.close() | |
print('get count', count[0]) | |
return count[0] | |
def deleteRow(self, id, table): | |
self.conn = sqlite3.connect('offline.db') | |
c = self.conn.cursor() | |
query = 'delete from '+table+' where id=?' | |
c.execute(query, (id.strip(),)) | |
self.conn.close() | |
print('row deleted', id) | |
print('row deleted table', table) | |
return True | |
def postData(): | |
print('post data now') | |
if self.getCount() > 0: | |
self.conn = sqlite3.connect('offline.db') | |
c = self.conn.cursor() | |
for row in c.exicute('select * from visitors'): | |
r = requests.post(self.store_face_url, data = row) | |
if 'data' in r.json: | |
self.deleteRow(row['id'], 'visitors') | |
for row in c.exicute('select * from errors'): | |
r = requests.post(self.store_error_url, data = row) | |
if 'data' in r.json: | |
self.deleteRow(row['id'], 'errors') | |
print('post data done') | |
return True | |
# offline = Offline() | |
# offline.getCount() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment