Esempio n. 1
0
    async def updateBeanDateList(beans, mysql):
        """一次性更新多个bean对象 讲道理结构是被破坏的,但是可以吧所有数据库请求压缩为一次"""

        conn, cur = await  mysql.getDatabaseConnected()

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

                    format_target = SqlUtils.getUpdateTableSetString(bean.getItemKeyList())
                    format_condition = SqlUtils.getQueryTableConditionString(bean.getIdentifyKeys())
                    sql = SqlUtils.STR_SQL_UPDATE_TABLE_UTILS.format(tableName, format_target, format_condition)
                    if configPraser.getPrintMode():
                        print(sql)

                    values = ()
                    for item in bean.getItemKeyList():
                        values = values + (valueDict.get(item, None),)

                    for item in bean.getIdentifyKeys():
                        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)
Esempio n. 2
0
    def updateValuesFromTable(tableName, targets, targetsDict, conditions, conditionsDict):
        """修改某张表"""

        conn = DataBaseOpenHelper.connect()

        cursor = conn.cursor()
        format_target = SqlUtils.getUpdateTableSetString(targets)
        format_condition = SqlUtils.getQueryTableConditionString(conditions)
        sql = SqlUtils.STR_SQL_UPDATE_TABLE_UTILS.format(tableName, format_target, format_condition)
        if configPraser.getPrintMode():
            print(sql)

        values = ()
        if targets is not None:
            for item in targets:
                values = values + (targetsDict.get(item, None),)

        if conditions is not None:
            for item in conditions:
                values = values + (conditionsDict.get(item, None),)  # 元组相加

        try:
            cursor.execute(sql, values)
            conn.commit()
        except Exception as e:
            print(e)
            conn.rollback()
        conn.close()
Esempio n. 3
0
    def queryValuesFromTable(tableName, items, valueDict):
        """查询数据库"""

        ret = []
        conn = DataBaseOpenHelper.connect()

        cursor = conn.cursor()
        format_values = SqlUtils.getQueryTableConditionString(items)
        sql = SqlUtils.STR_SQL_QUERY_TABLE_UTILS.format(tableName, format_values)
        if configPraser.getPrintMode():
            print(sql)

        values = ()
        if items is not None:
            for item in items:
                values = values + (valueDict.get(item, None),)  # 元组相加
        try:
            cursor.execute(sql, values)
            ret = cursor.fetchall()
            if configPraser.getPrintMode():
                print(ret)
        except Exception as e:
            print(e)
        conn.close()
        return ret
    async def queryBeanData(beans, mysql, defineItems=None):
        """一次性查询多个bean对象  define 为[[key1,key2], [key3,key4] ...]
        返回多个元组  [((),),((),())...]"""

        conn, cur = await mysql.getDatabaseConnected()

        resultBeans = []

        try:
            pos = 0
            for bean in beans:
                if isinstance(bean, BeanBase):
                    tableName = AsyncSqlHelper.getInsertTableName(bean)
                    items = defineItems[pos]
                    if items is None:
                        items = bean.getIdentifyKeys()
                    pos += 1
                    valueDict = bean.getValueDict()

                    format_values = SqlUtils.getQueryTableConditionString(
                        items)
                    sql = SqlUtils.STR_SQL_QUERY_TABLE_UTILS.format(
                        tableName, format_values)

                    if configPraser.getPrintMode():
                        print(sql)

                    values = ()
                    for item in items:
                        values = values + (valueDict.get(item, None), )  # 元组相加
                    try:
                        await cur.execute(sql, values)
                        r = await cur.fetchall()
                        resultBeans.append(r)
                    except Exception as e:
                        print(e)
                        resultBeans.append(None)
        except Exception as e:
            print(e)
        finally:
            if cur:
                await cur.close()
            await mysql.pool.release(conn)

        return resultBeans
Esempio n. 5
0
    def deleteValuesFromTable(tableName, items, valueDict):
        """删除某张表"""

        conn = DataBaseOpenHelper.connect()

        cursor = conn.cursor()
        format_values = SqlUtils.getQueryTableConditionString(items)
        sql = SqlUtils.STR_SQL_DELETE_TABLE_UTILS.format(tableName, format_values)
        if configPraser.getPrintMode():
            print(sql)

        values = ()
        if items is not None:
            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()