查看某個(gè)數(shù)據(jù)庫中的全部表:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '數(shù)據(jù)庫名'
因此查看某個(gè)庫中的某個(gè)表可以使用:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '數(shù)據(jù)庫名'
AND table_name = '表名稱';
在pymysql中,可以寫一個(gè)簡(jiǎn)單的工具函數(shù),用于查詢某個(gè)數(shù)據(jù)庫中是否包含某個(gè)表:文章來源:http://www.zghlxwxcb.cn/news/detail-702201.html
這里的_query函數(shù)請(qǐng)參考博客:python使用pymysql總是超時(shí)的解決方案文章來源地址http://www.zghlxwxcb.cn/news/detail-702201.html
from utils.sql_utils import _query
def sql_exists(database, table_name):
sql_info = _query(f"""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '{database}'
AND table_name = '{table_name}';
""", fetchone=True)
if sql_info is None:
return False # 不包含這個(gè)表
else:
return True # 包含這個(gè)表
到了這里,關(guān)于Mysql判斷某個(gè)數(shù)據(jù)庫中是否包含某個(gè)表,與pymysql工具函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!