Example #1
0
    def update(self,where='1=1', **kw): # set=None,where=None):
        """update a table row with set and where dictionaries:

           table.update_where({'x':1},{'y':'a'})
        translates to
           update TABLE set x=1 where y=a
           """
        if where==None or set==None:
            self.db.write("update must give 'where' and 'set' arguments")
            return
        try:
            s = []
            for k,v in kw.items():
                if self.fieldtypes.has_key(k):
                    ftype = self.fieldtypes[k]
                    k = clean_input(k)
                    if ftype == 'str':
                        s.append("%s=%s" % (k,safe_string(v)))
                    elif ftype in ('double','float'):
                        s.append("%s=%f" % (k,float(v)))
                    elif ftype == 'int':
                        s.append("%s=%i" % (k,int(v)))
            s = ','.join(s)
            q = "update %s set %s where %s" % (self._name,s,where)
            # print 'UPDATE Q ', q
            self.db.execute(q)
        except:
            self.db.write('update failed: %s' % q)
Example #2
0
 def select_where(self,**args):
     """check for a table row, and return matches"""
     if (self.check_args(**args)):
         q = "select * from %s where 1=1" % (self._name)
         for k,v in args.items():
             k = clean_input(k)
             v = safe_string(v)
             q = "%s and %s=%s" % (q,k,v)
         # print 'S WHERE ', q
         return self.db.exec_fetch(q)
     return 0
Example #3
0
 def insert(self,**args):
     "add a new table row "
     ok = self.check_args(**args)
     if (ok == 0):
         self.db.write("Bad argument for insert ")
         return 0
     q  = []
     for k,v in args.items():
         if self.fieldtypes.has_key(k):
             ftype = self.fieldtypes[k]
             field = clean_input(k.lower())
             if (v == None): v = ''
             if isinstance(v,(str,unicode)):
                 v = safe_string(v)
             else:
                 v = str(v)
             # v =clean_input(v,maxlen=flen)
             q.append("%s=%s" % (field, v))
     s = ','.join(q)
     qu = "insert into %s set %s" % (self._name,s)
     self.db.execute(qu)