Ejemplo n.º 1
0
    async def insertValuesIntoTable(self, tableName, items, valueDict, primaryKeys=None):
        """插入语句"""

        conn, cur = await self.getCursor()

        format_table = SqlUtils.getInsertTableFormatString(tableName, items)
        format_values = SqlUtils.getInsertTableValuesString(items.__len__())

        if configPraser.getPrintMode():
            print(format_table)
            print(format_values)

        sql = SqlUtils.STR_SQL_INSERT_TABLE_UTILS.format(format_table, format_values)
        if configPraser.getPrintMode():
            print(sql)

        values = ()
        for item in items:
            values = values + (valueDict.get(item, None),)  # 元组相加

        try:
            await cur.execute(sql, values)
        except Exception as e:
            print(e)
        finally:
            if cur:
                await cur.close()
            await self.pool.release(conn)
Ejemplo n.º 2
0
    async def storeBeanDateList(beans, mysql):
        """一次性存储多个bean对象 讲道理结构是被破坏的,但是可以吧所有数据库请求压缩为一次"""

        conn, cur = await  mysql.getDatabaseConnected()

        try:
            for bean in beans:
                if isinstance(bean, BeanBase):
                    tableName = AsyncSqlHelper.getInsertTableName(bean)
                    items = bean.getItemKeyList()
                    valueDict = bean.getValueDict()

                    format_table = SqlUtils.getInsertTableFormatString(tableName, items)
                    format_values = SqlUtils.getInsertTableValuesString(items.__len__())

                    sql = SqlUtils.STR_SQL_INSERT_TABLE_UTILS.format(format_table, format_values)
                    if configPraser.getPrintMode():
                        print(sql)

                    values = ()
                    for item in items:
                        values = values + (valueDict.get(item, None),)  # 元组相加
                    try:
                        await cur.execute(sql, values)
                    except Exception as e:
                        print(e)
        except Exception as e:
            print(e)
        finally:
            if cur:
                await cur.close()
            await mysql.pool.release(conn)
Ejemplo n.º 3
0
    def insertValuesIntoTable(tableName, items, valueDict, primaryKeys=None):
        """插入语句"""

        res = SqlExecuteHelper.queryValuesFromTable(tableName, primaryKeys, valueDict)
        if res is not None and res.__len__() > 0:
            if configPraser.getPrintMode():
                print('数据重复插入失败')
            return

        conn = DataBaseOpenHelper.connect()
        cursor = conn.cursor()
        format_table = SqlUtils.getInsertTableFormatString(tableName, items)
        format_values = SqlUtils.getInsertTableValuesString(items.__len__())

        if configPraser.getPrintMode():
            print(format_table)
            print(format_values)

        sql = SqlUtils.STR_SQL_INSERT_TABLE_UTILS.format(format_table, format_values)
        if configPraser.getPrintMode():
            print(sql)

        values = ()
        for item in items:
            values = values + (valueDict.get(item, None),)  # 元组相加
        try:
            cursor.execute(sql, values)
            conn.commit()
        except Exception as e:
            print(e)
            conn.rollback()
        conn.close()