Coverage for /Users/Dave/git_repos/_packages_/python/fundamentals/fundamentals/mysql/table_exists.py : 47%

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*Probe a database to determine if a given table exists*
6:Author:
7 David Young
9:Date Created:
10 June 21, 2016
11"""
12################# GLOBAL IMPORTS ####################
13import sys
14import os
15os.environ['TERM'] = 'vt100'
16from fundamentals import tools
17from fundamentals.mysql import readquery
20def table_exists(
21 dbConn,
22 log,
23 dbTableName):
24 """*Probe a database to determine if a given table exists*
26 **Key Arguments:**
27 - ``dbConn`` -- mysql database connection
28 - ``log`` -- logger
29 - ``dbTableName`` -- the database tablename
31 **Return:**
32 - ``tableExists`` -- True or False
34 **Usage:**
36 To test if a table exists in a database:
38 .. code-block:: python
40 from fundamentals.mysql import table_exists
41 exists = table_exists(
42 dbConn=dbConn,
43 log=log,
44 dbTableName="stupid_named_table"
45 )
47 print exists
49 # OUTPUT: False
50 """
51 log.debug('starting the ``table_exists`` function')
53 sqlQuery = u"""
54 SELECT count(*)
55 FROM information_schema.tables
56 WHERE table_name = '%(dbTableName)s'
57 """ % locals()
58 tableExists = readquery(
59 log=log,
60 sqlQuery=sqlQuery,
61 dbConn=dbConn,
62 quiet=False
63 )
65 if tableExists[0]["count(*)"] == 0:
66 tableExists = False
67 else:
68 tableExists = True
70 log.debug('completed the ``table_exists`` function')
71 return tableExists