'inserts rec dict using self.format into table (if given, else default or specified by field)
if insert fails, tries to create table and insert again before raising exception'

def insert(self, rec, table=None):

    if (not table):
        if ('table' in self.dbconfig):
            table = self.dbconfig['table']
        elif rec[self.tablefield]:
            table = rec[self.tablefield]
    try:
        sqlvalues = []
        cursor = self.dbconn.cursor()
        for (fname, ftype, fspec) in self.fields:
            if (fname in self.fieldmap):
                if (ftype in self.format_types):
                    sqlvalues.append(((('%' + fspec) + ftype) % rec[fname]))
                else:
                    sqlvalues.append(rec[fname])
        sql = (self.insertsql % table)
        self.log((sql + ('    %s' % sqlvalues)), logging.DEBUG)
    except:
        raise
    fail = False
    while True:
        try:
            cursor.execute(sql, sqlvalues)
            self.dbconn.commit()
            break
        except Exception as e:
            self.log(e, level=logging.WARNING)
            if fail:
                raise
            else:
                fail = True
                try:
                    self.createtable(table)
                except:
                    raise
