db service bug fixes

This commit is contained in:
Volkan Şahin 2016-03-16 10:40:17 +02:00
parent 4008aae6e6
commit fb16ab2e4f

View file

@ -16,7 +16,7 @@ class AhenkDbService(object):
self.connection=None self.connection=None
self.cursor = None self.cursor = None
def connect(): def connect(self):
try: try:
self.connection=sqlite3.connect(self.db_path, check_same_thread=False) self.connection=sqlite3.connect(self.db_path, check_same_thread=False)
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
@ -26,10 +26,15 @@ class AhenkDbService(object):
def check_and_create_table(self,table_name,cols): def check_and_create_table(self,table_name,cols):
if self.cursor: if self.cursor:
cols = ', '.join([str(x) for x in cols]) cols = ', '.join([str(x) for x in cols])
self.cursor.execute("create table if not exists "+table_name+" ("+cols+")") self.cursor.execute('create table if not exists '+table_name+' ('+cols+')')
else else:
self.warn("Could not create table cursor is None! Table Name : " + str(table_name)) self.warn("Could not create table cursor is None! Table Name : " + str(table_name))
def drop_table(self,table_name):
sql = "DROP TABLE "+table_name
self.cursor.execute(sql)
self.connection.commit()
def update(self, table_name, cols, args, criteria=None): def update(self, table_name, cols, args, criteria=None):
try: try:
if self.connection: if self.connection:
@ -50,7 +55,7 @@ class AhenkDbService(object):
self.connection.commit() self.connection.commit()
else: else:
self.warn("Could not update table cursor is None! Table Name : " + str(table_name)) self.warn("Could not update table cursor is None! Table Name : " + str(table_name))
except Exception, e: except Exception as e:
self.logger.error("Updating table error ! Table Name : " + str(table_name) + " " + str(e)) self.logger.error("Updating table error ! Table Name : " + str(table_name) + " " + str(e))
def delete(self): def delete(self):
@ -66,7 +71,7 @@ class AhenkDbService(object):
try: try:
if not cols == "*": if not cols == "*":
cols = ', '.join([str(x) for x in cols]) cols = ', '.join([str(x) for x in cols])
sql = "SELECT "+cols+" FROM " + table_name + " " + str(criteria) + " " + orderby sql = "SELECT "+cols+" FROM " + table_name + " where 1=1 and " + str(criteria) + " " + orderby
self.cursor.execute(sql) self.cursor.execute(sql)
rows = self.cursor.fetchall() rows = self.cursor.fetchall()
return rows return rows
@ -80,4 +85,4 @@ class AhenkDbService(object):
self.cursor.close() self.cursor.close()
self.connection.close() self.connection.close()
except Exception as e: except Exception as e:
self.logger.error("Closing database connection error " + str(e)) self.logger.error("Closing database connection error " + str(e))