Exemple #1
0
    def executemany(cls, sql, replace_vars=None):
        if not DB.conn:
            raise ConnectionError('mysql connection lost')

        result = None
        with DB.conn.cursor() as cursor:
            try:
                result = cursor.executemany(sql, replace_vars)
            except:
                Log.err('[sql execute error](' +
                        cursor.mogrify(sql, replace_vars) + ')')
        DB.conn.commit()

        return result
Exemple #2
0
    def insert(cls, table, data):
        if not DB.conn:
            raise ConnectionError('mysql connection lost')

        keys = ','.join(data.keys())
        values = ('%s,' * len(data))[0:-1]
        sql = "INSERT INTO %s (%s) VALUES (%s)" % (table, keys, values)
        replace_vars = tuple(data.values())
        with DB.conn.cursor() as cursor:
            try:
                result = cursor.execute(sql, replace_vars)
                DB.conn.commit()
                return result
            except:
                Log.err('[sql execute error](' +
                        cursor.mogrify(sql, replace_vars) + ')')