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

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*
6:Author:
7 David Young
9:Date Created:
10 June 21, 2016
11"""
12################# GLOBAL IMPORTS ####################
13from builtins import str
14import sys
15import os
16os.environ['TERM'] = 'vt100'
17from fundamentals import tools
18from fundamentals.mysql import readquery
21def get_database_table_column_names(
22 dbConn,
23 log,
24 dbTable
25):
26 """get database table column names
28 **Key Arguments:**
29 - ``dbConn`` -- mysql database connection
30 - ``log`` -- logger
31 - ``dbTable`` -- database tablename
33 **Return:**
34 - ``columnNames`` -- table column names
36 **Usage:**
38 To get the column names of a table in a given database:
40 .. code-block:: python
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')
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())
67 log.debug('completed the ``get_database_table_column_names`` function')
68 return columnNames