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 database connection and a database table name, return the column names for the table* 

5 

6:Author: 

7 David Young 

8""" 

9from builtins import str 

10import sys 

11import os 

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

13from fundamentals import tools 

14from fundamentals.mysql import readquery 

15 

16def get_database_table_column_names( 

17 dbConn, 

18 log, 

19 dbTable 

20): 

21 """get database table column names 

22 

23 **Key Arguments** 

24 

25 - ``dbConn`` -- mysql database connection 

26 - ``log`` -- logger 

27 - ``dbTable`` -- database tablename 

28  

29 

30 **Return** 

31 

32 - ``columnNames`` -- table column names 

33  

34 

35 **Usage** 

36 

37 To get the column names of a table in a given database: 

38 

39 ```python 

40 from fundamentals.mysql import get_database_table_column_names 

41 columnNames = get_database_table_column_names( 

42 dbConn=dbConn, 

43 log=log, 

44 dbTable="test_table" 

45 ) 

46 ``` 

47  

48 """ 

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

50 

51 sqlQuery = """SELECT * FROM %s LIMIT 1""" \ 

52 % (dbTable, ) 

53 # ############### >ACTION(S) ################ 

54 try: 

55 rows = readquery( 

56 log=log, 

57 sqlQuery=sqlQuery, 

58 dbConn=dbConn, 

59 ) 

60 except Exception as e: 

61 log.error( 

62 'could not find column names for dbTable %s - failed with this error: %s ' % 

63 (dbTable, str(e))) 

64 return -1 

65 columnNames = list(rows[0].keys()) 

66 

67 log.debug('completed the ``get_database_table_column_names`` function') 

68 return columnNames