Changeset - 5a7a122e2dbf
[Not reviewed]
0 1 0
Lance Edgar - 5 years ago 2019-08-06 18:25:14
ledgar@techsupport.coop
Add `mysql.table_exists()` convenience function
1 file changed with 10 insertions and 0 deletions:
0 comments (0 inline, 0 general) First comment
rattail_fabric2/mysql.py
Show inline comments
 
@@ -93,6 +93,16 @@ def grant_access(c, dbname, username):
 
    sql(c, 'grant all on `{}`.* to {}'.format(dbname, username))
 

	
 

	
 
def table_exists(c, tblname, dbname):
 
    """
 
    Determine if given table exists in given DB.
 
    """
 
    # TODO: should avoid sql injection here...
 
    query = "SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = '{}' AND TABLE_NAME = '{}'".format(dbname, tblname)
 
    name = sql(c, query, database='information_schema').stdout.strip()
 
    return name == tblname
 

	
 

	
 
def sql(c, sql, database=''):
 
    """
 
    Execute some SQL.
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now