How to check if table exists in MySQL database with Python

1 Answer

0 votes
# c:\Users\user_nm\AppData\Local\Programs\Python\Python35-32\Scripts\pip install mysql-connector
 
import mysql.connector
 
db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="",
    database="python_database"
)

db_cursor = db.cursor()

db_cursor.execute("SHOW TABLES")

for tbl_name in db_cursor:
    if tbl_name[0] == 'workers':
        print(tbl_name[0])

'''
run:

workers

'''

 



answered Nov 28, 2018 by avibootz
...