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*Probe a database to determine if a given table exists* 

5 

6:Author: 

7 David Young 

8 

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 

18 

19 

20def table_exists( 

21 dbConn, 

22 log, 

23 dbTableName): 

24 """*Probe a database to determine if a given table exists* 

25 

26 **Key Arguments:** 

27 - ``dbConn`` -- mysql database connection 

28 - ``log`` -- logger 

29 - ``dbTableName`` -- the database tablename 

30 

31 **Return:** 

32 - ``tableExists`` -- True or False 

33 

34 **Usage:** 

35 

36 To test if a table exists in a database: 

37 

38 .. code-block:: python  

39 

40 from fundamentals.mysql import table_exists 

41 exists = table_exists( 

42 dbConn=dbConn, 

43 log=log, 

44 dbTableName="stupid_named_table" 

45 ) 

46 

47 print exists 

48 

49 # OUTPUT: False 

50 """ 

51 log.debug('starting the ``table_exists`` function') 

52 

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 ) 

64 

65 if tableExists[0]["count(*)"] == 0: 

66 tableExists = False 

67 else: 

68 tableExists = True 

69 

70 log.debug('completed the ``table_exists`` function') 

71 return tableExists