executeQuery.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.db import connection, transaction
  2. '''执行django原始sql语句 并返回一个数组对象'''
  3. # b = 'MO1505000001'
  4. # sql = ("SELECT TOP 5 MO_NO FROM MF_MO WHERE MO_NO='%s'"%(b))
  5. # print(sql)
  6. #
  7. # b1 = 1
  8. # sql1 = ("SELECT TOP 5 MO_NO FROM MF_MO WHERE MO_NO=%d"%(b1))
  9. # print(sql1)
  10. def executeQuery(sql):
  11. """"
  12. sql:sql语句
  13. """
  14. with connection.cursor() as cursor:
  15. print(sql)
  16. dep_data = cursor.execute(sql, []).fetchall()
  17. col_names = [desc[0] for desc in cursor.description]
  18. result = []
  19. for row in dep_data:
  20. objDict = {}
  21. # 把每一行的数据遍历出来放到Dict中
  22. for index, value in enumerate(row):
  23. objDict[col_names[index]] = value
  24. result.append(objDict)
  25. return result
  26. def IseUpDelQuery(sql):
  27. """"
  28. sql:要执行增加删除的sql语句
  29. """
  30. with connection.cursor() as cursor:
  31. cursor.execute(sql)
  32. def rpoold(sql):
  33. """用于判断单号是否重复"""
  34. with connection.cursor() as cursor:
  35. data = cursor.execute(sql, []).fetchall()[0][0]
  36. return data