Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/local/bin/python 

2# encoding: utf-8 

3""" 

4*Given a mysql query, read the data from the database and return the results as a list of dictionaries (database rows)* 

5 

6:Author: 

7 David Young 

8""" 

9from builtins import str 

10import sys 

11import os 

12os.environ['TERM'] = 'vt100' 

13from fundamentals import tools 

14 

15 

16def readquery( 

17 sqlQuery, 

18 dbConn, 

19 log, 

20 quiet=False): 

21 """Given a mysql query, read the data from the database and return the results as a list of dictionaries (database rows) 

22 

23 **Key Arguments** 

24 

25 - ``log`` -- the logger. 

26 - ``sqlQuery`` -- the MySQL command to execute 

27 - ``dbConn`` -- the db connection 

28 - ``quiet`` -- ignore mysql warnings and errors and move on. Be careful when setting this to true - damaging errors can easily be missed. Default *False*. 

29 

30 

31 **Return** 

32 

33 - ``rows`` -- the rows returned by the sql query 

34 

35 

36 **Usage** 

37 

38 ```python 

39 from fundamentals.mysql import readquery 

40 rows = readquery( 

41 log=log, 

42 sqlQuery=sqlQuery, 

43 dbConn=dbConn, 

44 quiet=False 

45 ) 

46 ``` 

47 

48 """ 

49 log.debug('starting the ``readquery`` function') 

50 import pymysql 

51 import warnings 

52 warnings.filterwarnings('error', category=pymysql.Warning) 

53 

54 rows = [] 

55 

56 log.debug("\nSQLQUERY: %(sqlQuery)s}\n" % locals()) 

57 

58 try: 

59 cursor = dbConn.cursor(pymysql.cursors.DictCursor) 

60 except Exception as e: 

61 log.error('could not create the database cursor: %s' % (e, )) 

62 raise IOError('could not create the database cursor: %s' % (e, )) 

63 # EXECUTE THE SQL COMMAND 

64 tryAgain = True 

65 tries = 1 

66 while tryAgain: 

67 tryAgain = False 

68 try: 

69 cursor.execute(sqlQuery) 

70 rows = cursor.fetchall() 

71 except pymysql.err.InternalError as e: 

72 if tries < 61: 

73 tryAgain = True 

74 log.warning(f"MySQL error: {e}. Attempt {tries}/60.") 

75 tries += 1 

76 else: 

77 log.warning(f"MySQL error: {e}. Attempt {tries}/60 failed. ") 

78 raise 

79 except Exception as e: 

80 sqlQuery = sqlQuery[:1000] 

81 if quiet == False: 

82 log.warning( 

83 'MySQL raised an error - read command not executed.\n' + str(e) + '\nHere is the sqlQuery\n\t%(sqlQuery)s' % locals()) 

84 raise e 

85 else: 

86 log.warning( 

87 'MySQL raised an error - read command not executed.\n' + str(e) + '\nHere is the sqlQuery\n\t%(sqlQuery)s' % locals()) 

88 pass 

89 

90 # CLOSE THE CURSOR 

91 try: 

92 cursor.close() 

93 except Exception as e: 

94 log.warning('could not close the db cursor ' + str(e) + '\n') 

95 

96 log.debug('completed the ``readquery`` function') 

97 return rows