def set_default_table(self, table):
self.table = table
- def add_entry(self, dict, table=None):
+ def add_entry(self, dict_, table=None):
if table == None:
table = self.table
- dict = self._clean_dict(dict)
- keys = dict.keys()
- values = ["'%s'" % dict[key] for key in keys]
+ dict_ = self._clean_dict(dict_)
+ keys = dict_.keys()
+ values = ["'%s'" % dict_[key] for key in keys]
self.execute("INSERT INTO %s (%s) VALUES (%s)"
% (table, ', '.join(keys), ', '.join(values)))
# error checking
- def list_entries(self, dict=None, table=None):
+ def list_entries(self, dict_=None, table=None):
if table == None:
table = self.table
- where = self._where_string(dict)
+ where = self._where_string(dict_)
self.execute('SELECT * FROM %s%s' % (table, where))
numrows = int(self.cursor.rowcount)
- print '\t'.join([dtuple[0] for dtuple in self.cursor.description])
+ fields = [dtuple[0] for dtuple in self.cursor.description]
+ ret = []
for x in range(0,numrows):
row = self.cursor.fetchone()
- print '\t'.join(['%s' % x for x in row])
+ ret.append(dict(zip(fields, row)))
+ return ret
- def delete_entries(self, dict, table=None):
+ def delete_entries(self, dict_, table=None):
if table == None:
table = self.table
- where = self._where_string(dict)
+ where = self._where_string(dict_)
self.execute('DELETE FROM %s%s' % (table, where))
- def _clean_dict(self, dict=None):
- if dict == None:
+ def _clean_dict(self, dict_=None):
+ if dict_ == None:
return None
- for key in dict.keys():
- value = dict.pop(key)
+ for key in dict_.keys():
+ value = dict_.pop(key)
value = MySQLdb.escape_string(value)
key = MySQLdb.escape_string(key)
- assert key not in dict, '%s in %s!' % (key, dict)
- dict[key] = value
- return dict
+ assert key not in dict_, '%s in %s!' % (key, dict_)
+ dict_[key] = value
+ return dict_
- def _where_string(self, dict=None):
- dict = self._clean_dict(dict)
- if dict == None or len(dict) == 0:
+ def _where_string(self, dict_=None):
+ dict_ = self._clean_dict(dict_)
+ if dict_ == None or len(dict_) == 0:
where = ''
else:
where_args = []
- for key,value in dict.items():
+ for key,value in dict_.items():
where_args.append("%s='%s'" % (key,value))
where = ' where %s' % ' and '.join(where_args)
return where