19 self.
conn = sqlite3.connect(db_filename)
25 self.conn.execute(
"PRAGMA foreign_keys = ON")
40 self.cursor.executescript(stmt)
43 except Exception
as err:
44 logging.error(
'Error executing SQL statement "%s":\n%s' % (stmt, err))
53 sql_file = open(filename,
'r')
56 self.cursor.executescript(sql)
60 except Exception
as err:
61 logging.error(
'Error executing SQL script "%s":\n%s' % (filename, err))
72 if table
and len(rows) > 0:
76 sql =
'INSERT INTO %s (%s) VALUES (%s);' % (table,
','.join(cols),
'?,' * (num_cols - 1) +
'?');
80 for i
in range(num_rows):
81 self.cursor.execute(sql, rows[i])
82 last_ids.append(int(self.cursor.lastrowid))
87 except Exception
as error:
88 logging.error(
'Database error on insert()')
89 logging.error(
'Statement: %s' % (sql))
90 logging.error(
"Stack trace: %s" % (traceback.format_exc()))
104 def select(self, table, cols, where_cond=None, params=[], order_by=None, dump_sql=False, group_by=None):
105 sql =
'SELECT %s FROM %s' % (
','.join(cols), table)
107 sql +=
' WHERE %s' % (where_cond)
110 sql +=
' GROUP BY %s' % (group_by)
113 sql +=
' ORDER BY %s' % (order_by)
121 self.cursor.execute(sql, params)
123 except Exception
as error:
124 logging.error(
'Database error on select()')
125 logging.error(
'Query: %s' % (sql))
126 logging.error(
"Stack trace: %s" % (traceback.format_exc()))
128 return self.cursor.fetchall()
136 def delete(self, table, where_cond=None, params=[]):
137 sql =
'DELETE FROM %s' % (table)
139 sql +=
' WHERE %s' % (where_cond)
145 self.cursor.execute(sql, params)
146 rowcount = self.cursor.rowcount
149 except Exception
as error:
150 print 'error on delete!'
151 logging.error(
'Database error on delete()')
152 logging.error(
'Statement: %s' % (sql))
153 logging.error(
"Stack trace: %s" % (traceback.format_exc()))
166 sql =
'UPDATE %s SET %s=CURRENT_TIMESTAMP' % (table, col)
168 sql +=
' WHERE %s' % (where_cond)
174 self.cursor.execute(sql, params)
175 rowcount = self.cursor.rowcount
178 except Exception
as error:
179 logging.error(
'Database error on update_timestamp_col()')
180 logging.error(
'Statement: %s' % (sql))
181 logging.error(
"Stack trace: %s" % (traceback.format_exc()))
193 def update(self, table, cols, where_cond=None, params=[], dump_sql=False):
195 sql =
'UPDATE %s SET ' % (table)
197 for i
in range(len(cols)):
198 sql +=
'%s=?' % cols[i]
199 if i < len(cols) - 1:
203 sql +=
' WHERE %s' % (where_cond)
211 self.cursor.execute(sql, params)
212 rowcount = self.cursor.rowcount
215 except Exception
as error:
216 logging.error(
'Database error on update()')
217 logging.error(
'Statement: %s' % (sql))
218 logging.error(
"Stack trace: %s" % (traceback.format_exc()))
228 file_out = open(path,
'wb')
229 writer = csv.writer(file_out)
231 self.cursor.execute(
'PRAGMA table_info(%s)' % (table))
232 rows = self.cursor.fetchall()
235 col_names = map(
lambda cur_row: cur_row[1], rows)
238 writer.writerow(col_names)
241 rows = self.
select(table, col_names, order_by=
'id')
243 writer.writerow(cur_row)