from django.db import connection, transaction
'''执行django原始sql语句  并返回一个数组对象'''

# b = 'MO1505000001'
# sql = ("SELECT TOP 5 MO_NO FROM MF_MO WHERE MO_NO='%s'"%(b))
# print(sql)
#
# b1 = 1
# sql1 = ("SELECT TOP 5 MO_NO FROM MF_MO WHERE MO_NO=%d"%(b1))
# print(sql1)
def executeQuery(sql):
    """"
    sql:sql语句
    """
    with connection.cursor() as cursor:
        dep_data = cursor.execute(sql, []).fetchall()
        col_names = [desc[0] for desc in cursor.description]
        result = []
        for row in dep_data:
            objDict = {}
            # 把每一行的数据遍历出来放到Dict中
            for index, value in enumerate(row):
                objDict[col_names[index]] = value
            result.append(objDict)
        return result
