当前位置:网站首页>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
边栏推荐
- After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
- Introduction to message queuing (MQ)
- What functions need to be set after the mall system is built
- 论文阅读_清华ERNIE
- [XSS bypass - protection strategy] understand the protection strategy and better bypass
- Leetcode simple question: check whether the string is an array prefix
- How to choose cross-border e-commerce multi merchant system
- Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
- A outsourcing boy's mid-2022 summary
- Dive into deep learning - 2.1 data operation & Exercise
猜你喜欢
Triangular rasterization
7. Integrated learning
Basic use of continuous integration server Jenkins
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
逆袭大学生的职业规划
SSM based campus part-time platform for College Students
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
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
《牛客刷verilog》Part II Verilog进阶挑战
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
随机推荐
Market status and development prospects of the global IOT active infrared sensor industry in 2022
Some information about the developer environment in Chengdu
Ffmpeg tanscoding transcoding
雇佣收银员(差分约束)
Review the old and know the new: Notes on Data Science
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Human resource management system based on JSP
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
Youdao cloud notes
Integration of Android high-frequency interview questions (including reference answers)
[set theory] binary relation (example of binary relation on a | binary relation on a)
Shell script Basics - basic grammar knowledge
[XSS bypass - protection strategy] understand the protection strategy and better bypass
document. The problem of missing parameters of referer is solved
逆袭大学生的职业规划
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Priv app permission exception
Thesis reading_ ICD code_ MSMN
Kingbasees plug-in KDB of Jincang database_ database_ link
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found