Coverage for /Users/Dave/git_repos/_packages_/python/fundamentals/fundamentals/mysql/get_database_table_column_names.py: 44%

# Coverage for /Users/Dave/git_repos/_packages_/python/fundamentals/fundamentals/mysql/get_database_table_column_names.py : 44%

## 18 statements 8 run 10 missing 0 excluded

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

9:Date Created:

10 June 21, 2016

11"""

12################# GLOBAL IMPORTS ####################

13 from builtins import str

14 import sys

15 import os

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

17 from fundamentals import tools

18 from fundamentals.mysql import readquery

19

20

21 def get_database_table_column_names(

22 dbConn,

23 log,

24 dbTable

25):

26 """get database table column names

27

28 **Key Arguments:**

29 - ``dbConn`` -- mysql database connection

30 - ``log`` -- logger

31 - ``dbTable`` -- database tablename

32

33 **Return:**

34 - ``columnNames`` -- table column names

35

36 **Usage:**

37

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

39

40 .. code-block:: python

41

42 from fundamentals.mysql import get_database_table_column_names

43 columnNames = get_database_table_column_names(

44 dbConn=dbConn,

45 log=log,

46 dbTable="test_table"

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