如何使用Python在 SQL 中查找包含特定列的表?
MySQL服务器是一个开源的关系数据库管理系统,是对基于 Web 的应用程序的主要支持。数据库和相关表格是许多网站和应用程序的主要组成部分,因为数据是通过网络存储和交换的。为了从 Web 服务器访问 MySQL 数据库,我们使用Python中的各种模块,例如 PyMySQL、mysql.connector 等。
在本文中,我们将获得具有特定列的表,而与数据库无关。首先,我们将连接到服务器。将要使用的 SQL 查询是:
select tab.table_schema as database_name,tab.table_name
from information_schema.tables as tab
inner join information_schema.columns as col
on col.table_schema = tab.table_schema
and col.table_name = tab.table_name
and column_name = 'Specified_Column_Name'
where tab.table_type = 'BASE TABLE'
order by tab.table_schema, tab.table_name;
下面是一些描述如何获取具有特定列的 MySQL 表的程序:
示例 1
下面是所有表的表描述,示例数据库具有以下表,这些表将被Python脚本访问:
下面是获取具有特定列的 MySQL 表的详细信息的程序:
Python3
# Module Imports
import mysql.connector as mariadb
import sys
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user="root",
password="",
database="gfg"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get cursor object
cur = conn.cursor()
# Get the table and database details having a particular column
cur.execute("select tab.table_schema as database_name,tab.table_name \
from information_schema.tables as tab \
inner join information_schema.columns as col \
on col.table_schema = tab.table_schema \
and col.table_name = tab.table_name \
and column_name = 'Worker_ID' \
where tab.table_type = 'BASE TABLE' \
order by tab.table_schema, tab.table_name;")
# Display the tables
for(database_name, tab) in cur:
print(f"Table Name: {tab}Database Name: {database_name}")
Python3
# Module Imports
import mysql.connector as mariadb
import sys
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user="root",
password="",
database="gfg"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get cursor object
cur = conn.cursor()
# Get the table and database details having a particular column
cur.execute("select tab.table_schema as database_name,tab.table_name \
from information_schema.tables as tab \
inner join information_schema.columns as col \
on col.table_schema = tab.table_schema \
and col.table_name = tab.table_name \
and column_name = 'Names' \
where tab.table_type = 'BASE TABLE' \
order by tab.table_schema, tab.table_name;")
# Display the tables
for(database_name, tab) in cur:
print(f"\n\nTable Name: {tab}\nDatabase Name: {database_name}")
输出:
示例 2
这是获取表详细信息的另一个示例,无论数据库是否具有特定列,以下是所有表的描述:
以下是用于获取具有特定列名称的表详细信息的Python脚本:
蟒蛇3
# Module Imports
import mysql.connector as mariadb
import sys
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user="root",
password="",
database="gfg"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get cursor object
cur = conn.cursor()
# Get the table and database details having a particular column
cur.execute("select tab.table_schema as database_name,tab.table_name \
from information_schema.tables as tab \
inner join information_schema.columns as col \
on col.table_schema = tab.table_schema \
and col.table_name = tab.table_name \
and column_name = 'Names' \
where tab.table_type = 'BASE TABLE' \
order by tab.table_schema, tab.table_name;")
# Display the tables
for(database_name, tab) in cur:
print(f"\n\nTable Name: {tab}\nDatabase Name: {database_name}")
输出: