当前位置:网站首页>Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
2022-07-03 04:46:00 【The fate of the sleepers】
Use sqlalchemy The module obtains the table name and field name of the existing table in the database
Recently used Python in ORM modular , for example SQLalchemy To read tables and related data in the database , At first contact , Most of the time, they imitate other bloggers or official cases on the Internet , Fine tune the code for the database you need to operate , Then I have some experience in the process of use , Close up this article only for experience sharing .
/ Module and database
windows 10
python 3.7
sqlalchemy 1.3.13
mysql-connector 8.0.28
mysql 8.0.18
/ brief introduction
By comparison operation mysql Under the sql Grammar and orm The similarities and differences of grammar learning in reading data tables .
/ mysql grammar
Connect to database :
Use the command line to enter the database , If not mysql.exe Set in the system environment , Please switch the command line path to mysql.exe Location path ,Ctrl+R Then type in cmd Go to the command line .
mysql -u root -p
Enter password: password
Display all databases :
show databases;
Using a database :
use database; -- database: Enter your own database name
Query the data of the data table :
select * from table; -- table: Target data table
The above is the use of sql Syntax uses the database to query the data of the data table under the current table .
/ ORM(SQLalchemy) The way
Use sqlalchemy when , If there is no corresponding table in the current library, a new table model class will be created , Take this as the query condition , Of course, it has been explained in this article that there are tables , Of course, you can complete the operation of obtaining data tables without establishing model classes .
I found it useful this time pymysql As a connector, there are always warnings , Switch to mysqlconnector Just fine .
pip intall mysql-connector-python
Create table model classes
from sqlalchemy import create_engine, MetaData, Column
from sqlalchemy.dialects.mysql import INTEGER, DOUBLE, BIGINT, VARCHAR, CHAR, TEXT, DATETIME
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Create database engine ,ps: There is no connection to the specific database
engine = create_engine("mysql+mysqlconnector://root:[email protected]:3306", pool_recycle=7200)
# Create base class , Use MetaData Module building library model metadata class
Base = declarative_base(bind=engine, metadata=MetaData(schema='dash_base'))
# Use sessionmaker Keep the database session connected
session = sessionmaker(bind=engine)()
class Tips(Base):
""" Inherited base class , Design the specific column names or formats in the table """
__tablename__ = 'tips'
idx = Column('idx', BIGINT(20), primary_key=True)
total_bill = Column('total_bill', DOUBLE())
tip = Column('tip', DOUBLE())
sex = Column('sex', TEXT())
smoker = Column('smoker', TEXT())
day = Column('day', TEXT())
time = Column('time', TEXT())
size = Column('size', BIGINT(20))
Query all the data
[tips.__dict__ for tips in session.query(Tips).all()]

Query partial column data
session.query(Tips.total_bill, Tips.tip).all()

Sometimes it will combine pandas Use it together .
import pandas as pd
# table name ,con: The link of parameters should be specific to the database name
pd.read_sql(Tips.__tablename__, con="mysql+mysqlconnector://root:[email protected]:3306/dash_base")

( Manual watermark : original CSDN The fate of the sleepers ,https://blog.csdn.net/weixin_46281427?spm=1011.2124.3001.5343, official account A11Dot send )
There is no difficulty in understanding the above code , The main reason is that you need to manually type in the table model class every time , It will be troublesome to write the existing table like this again , If the number of table fields increases , The number of tables to query increases , Will want to find a better solution , Of course there are , Please read on .
Use insepctor Module and MetaData class
insepctor There are two ways to create :
- Mode one
from sqlalchemy.engine import reflection
engine = create_engine("mysql+mysqlconnector://root:[email protected]:3306", pool_recycle=7200)
insp = reflection.Inspector.from_engine(engine)
- Mode two
from sqlalchemy import create_engine, inspect, MetaData, Table
from sqlalchemy.orm import sessionmaker
engine = create_engine("mysql+mysqlconnector://root:[email protected]:3306", pool_recycle=7200)
insp = inspect(engine)
Get database name
insp.get_schema_names()

Get the table name under the current database
insp.get_table_names(schema='dash_base') # schema: Database name

Get the column name of the current data table
inspect_resp.get_columns('tips', schema='dash_base') # Table name , Library name

name Value is column name ,type Value is the type of column . However, this method of obtaining data is not helpful for querying the data in the table , The reason is that it cannot be used str Type as session.query() The object of .
Next use MetaData Class connection database mapping the table structure in the delivery .
Get the database name in the previous step , Select a database name and fill it in schema Parameters in .
metadata = MetaData(bind=engine)
# reflect mapping dash_base Table structure under the Library
metadata.reflect(schema='dash_base')
After passing this code , You can use the following tables Method to get the table name and table structure :
Table name :
Table structure :
You can also use Table Class to create a new table structure :
from sqlalchemy import Table
Table('tips', metadata, schema='dash_base')

I recommend using a dictionary in the form of metadata.tables Analyze it , Convenient retrieval , You don't have to use it anymore Table() class .
table_dicts = {
i.name: i for i in metadata.tables.values()}
And then get the Field name :
table_dicts['tips'].c.keys()
# ['idx', 'total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size']
The field name obtained in this way uses session.query A query is a list of data , Instead of iterators .
Say an extra sentence , have access to Base.metadata Instead of MetaData, Same effect .
summary
Every time I use a module, I will feel a little more , Only by recording in time can we better understand and use this module , At first, every time I want to query through this module, I have to create a table model class , Also on the other hand, spit out that the design of this module is unreasonable , If you don't know the specific design of the table structure, how to query , Through the accumulation of experience , Slowly feel orm The model design is so robust , So convenient .
The world is full of flowers and grass , Why not raise a glass and cheer for the endless beauty !
Made on February 14th, 2002
边栏推荐
- Leetcode simple question: check whether two string arrays are equal
- data2vec! New milestone of unified mode
- C Primer Plus Chapter 10, question 14 3 × 5 array
- 4 years of experience to interview test development, 10 minutes to end, ask too
- Market status and development prospect prediction of global SoC Test Platform Industry in 2022
- [free completion] development of course guidance platform (source code +lunwen)
- Leetcode simple question: the key with the longest key duration
- 【SQL注入】联合查询(最简单的注入方法)
- [USACO 2009 Dec S]Music Notes
- Human resource management system based on JSP
猜你喜欢

Triangular rasterization

SSM based campus part-time platform for College Students

Introduction to JVM principle

论文阅读_中文NLP_ELECTRA

How to choose cross-border e-commerce multi merchant system

Web security - CSRF (token)

MC Layer Target

Thesis reading_ Chinese NLP_ ELECTRA

JVM原理简介

I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
随机推荐
JVM原理简介
Market status and development prospect prediction of the global fire extinguisher industry in 2022
[free completion] development of course guidance platform (source code +lunwen)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Priv app permission exception
I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
I stepped on a foundation pit today
Market status and development prospects of the global IOT active infrared sensor industry in 2022
Pyqt control part (II)
Concurrent operation memory interaction
Leetcode simple question: the key with the longest key duration
Hj35 serpentine matrix
stm32逆向入门
[set theory] binary relation (example of binary relation on a | binary relation on a)
Arthas watch grabs a field / attribute of the input parameter
Two drawing interfaces - 1 Matlab style interface
Employee attendance management system based on SSM
[SQL injection] joint query (the simplest injection method)
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
Contents of welder (primary) examination and welder (primary) examination in 2022