pull/10/head
Steve Nyemba 10 months ago
parent c3ebd32a40
commit 0f60c748ba

@ -211,18 +211,18 @@ class SQLiteWriter(SQLite,DiskWriter) :
# #
# If the table doesn't exist we should create it # If the table doesn't exist we should create it
# #
def write(self,_data,**_args): # def write(self,_data,**_args):
SQLiteWriter.LOCK.acquire() # SQLiteWriter.LOCK.acquire()
try: # try:
if type(_data) == dict : # if type(_data) == dict :
_data = [_data] # _data = [_data]
_table = self.table if 'table' not in _args else _args['table'] # _table = self.table if 'table' not in _args else _args['table']
_df = pd.DataFrame(_data) # _df = pd.DataFrame(_data)
_df.to_sql(_table,self._engine.connect(),if_exists='append',index=False) # _df.to_sql(_table,self._engine.connect(),if_exists='append',index=False)
except Exception as e: # except Exception as e:
print (e) # print (e)
SQLiteWriter.LOCK.release() # SQLiteWriter.LOCK.release()
def _write(self,info,**_args): def write(self,info,**_args):
""" """
""" """
@ -247,17 +247,23 @@ class SQLiteWriter(SQLite,DiskWriter) :
cursor = self.conn.cursor() cursor = self.conn.cursor()
sql = " " .join(["INSERT INTO ",self.table,"(", ",".join(self.fields) ,")", "values(:values)"]) sql = " " .join(["INSERT INTO ",self.table,"(", ",".join(self.fields) ,")", "values(:values)"])
for row in info : for row in info :
stream =["".join(["",value,""]) if type(value) == str else value for value in row.values()] values = [ str(row[field]) if type(row[field]) not in [list,dict] else json.dumps(row[field],cls=IEncoder) for field in self.fields]
stream = json.dumps(stream,cls=IEncoder) values = ["".join(["'",value,"'"]) for value in values]
stream = stream.replace("[","").replace("]","")
# stream =["".join(["",value,""]) if type(value) == str else value for value in row.values()]
# stream = json.dumps(stream,cls=IEncoder)
# stream = stream.replace("[","").replace("]","")
self.conn.execute(sql.replace(":values",stream) ) # print (sql.replace(":values",stream))
# self.conn.execute(sql.replace(":values",stream) )
self.conn.execute(sql.replace(":values", ",".join(values)) )
# cursor.commit() # cursor.commit()
self.conn.commit() self.conn.commit()
# print (sql) # print (sql)
except Exception as e : except Exception as e :
print ()
print (e) print (e)
pass pass
SQLiteWriter.LOCK.release() SQLiteWriter.LOCK.release()

Loading…
Cancel
Save