Simple function for inserting a dictionary whose keys match the fieldnames of tblname. The function returns two values, the
            second of which is a dict containing the primary keys of the record. If a record already exists then no insertion is performed and
            (False, the dictionary of existing primary keys) is returned. Otherwise, the record is inserted into the database and (True, d)
            is returned.

def insertDictIfNew(self, tblname, d, PKfields, fields=None, locked = True):
        '''Simple function for inserting a dictionary whose keys match the fieldnames of tblname. The function returns two values, the
            second of which is a dict containing the primary keys of the record. If a record already exists then no insertion is performed and
            (False, the dictionary of existing primary keys) is returned. Otherwise, the record is inserted into the database and (True, d)
            is returned.'''
        SQL, values = self._insert_dict_if_new_inner(tblname, d, PKfields, fields=fields, locked = locked)
        if SQL != False:
            if locked:
                self.locked_execute(SQL, parameters=values)
            else:
                self.execute(SQL, parameters=values, locked = False)
            return True, d
        return False, values

        # todo: remove the code below
        self.queries_run += 1
        if type(PKfields) == type(""):
            PKfields = [PKfields]

        if fields == None:
            fields = sorted(d.keys())
        values = None
        SQL = None
        try:
            # Search for existing records
            wherestr = []
            PKvalues = []
            for PKfield in PKfields:
                if d[PKfield] == None:
                    wherestr.append("%s IS NULL" % PKfield)
                else:
                    wherestr.append("%s=%%s" % PKfield)
                    PKvalues.append(d[PKfield])
            PKfields = join(PKfields, ",")
            wherestr = join(wherestr, " AND ")
            existingRecord = self.execute("SELECT %s FROM %s" % (PKfields, tblname) + " WHERE %s" % wherestr,
                                          parameters=tuple(PKvalues), locked = locked)
            if existingRecord:
                return False, existingRecord[0]

            SQL = 'INSERT INTO %s (%s) VALUES (%s)' % (
            tblname, join(fields, ", "), join(['%s' for x in range(len(fields))], ','))
            values = tuple([d[k] for k in fields])
            self.execute(SQL, parameters=values, locked = locked)
            return True, d
        except Exception, e:
            if SQL and values:
                sys.stderr.write("\nSQL execution error in query '%s' %% %s at %s:" % (
                SQL, values, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
            sys.stderr.write("\nError: '%s'.\n" % (str(e)))
            sys.stderr.flush()
            raise Exception("Error occurred during database insertion: '%s'. %s" % (str(e), traceback.format_exc()))