[PYTHON] Get table dynamically with sqlalchemy

Get table dynamically with sqlalchemy

The version I tried.

In [1]: import sqlalchemy

In [2]: sqlalchemy.__version__
Out[2]: '1.0.5'

If you pass Metadata or engine to sqlalchemy.Table (), you can get the Table object that can be passed to session as it is.

Make a dict of {<table name>: sqlalchemy.Table} and place it in a wide area of the module.


# -*- coding: utf-8 -*-

import sqlalchemy
import sqlalchemy.ext.declarative
Base = sqlalchemy.ext.declarative.declarative_base()

url = 'mysql://[email protected]/employees?charset=utf8'

engine = sqlalchemy.create_engine(url, echo=False)
tables = { name: sqlalchemy.Table(name, Base.metadata, autoload=True, autoload_with=engine)
        for name in engine.table_names()


Session = sqlalchemy.orm.sessionmaker(bind=engine)
session = Session()

ʻEmployees` Get all records from the table. (MySQL sample)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dbs

for x in dbs.session.query(dbs.employees):
    print x

I wonder if it's so dynamic

TODO: You might want to export it in the form of a static class.

You can dynamically lick the table and spit it out like this.

Such a shape

class Student(Base):
    __tablename__ = 'students'

    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    name = sqlalchemy.Column(sqlalchemy.String(255))
    kana = sqlalchemy.Column(sqlalchemy.String(255))

    def __repr__(self):
        return '<name=%s, kana=%s>' % (self.name, self.kana)

Recommended Posts

Get table dynamically with sqlalchemy
How to get parent id with sqlalchemy
Table definition in SQLAlchemy
Get started with MicroPython
Get Tweets with Tweepy
Get date with python
Get started with Mezzanine
Use Enums with SQLAlchemy
sqlalchemy table definition tips
How to read dynamically generated table definitions using SQLAlchemy
How to get more than 1000 data with SQLAlchemy + MySQLdb
Get country code with python
Get started with Django! ~ Tutorial ⑤ ~
Get Twitter timeline with python
Create / search / create table with PynamoDB
Get started with influxDB + Grafana
Use DATE_FORMAT with SQLAlchemy filter
Get Youtube data with python
Get information with zabbix api
Get started with Django! ~ Tutorial ④ ~
Introduction to RDB with sqlalchemy Ⅰ
How to update with SQLAlchemy?
Browse columns encrypted with sqlalchemy
Get started with Django! ~ Tutorial ⑥ ~
Get thread ID with python
How to Alter with SQLAlchemy?
Dynamically move Amplify with Lambda
Get started with Python! ~ ② Grammar ~
Get image features with OpenCV
group_by with sqlalchemy and sum
Get stock price with Python
Support multi session with SQLAlchemy
Get home directory with python
Get keyboard events with python
Table scraping with Beautiful Soup
How to Delete with SQLAlchemy?
Get Alembic information with Python
Get another tab with pyppeteer
Get ranking with Rakuten API
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
A note I was addicted to when creating a table with SQLAlchemy
Get video file information with ffmpeg-python
Use Azure SQL Database with SQLAlchemy
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Connect to multiple databases with SQLAlchemy
Get negative reaction time with psychopy.event.getKeys ()
Accelerate query generation with SQLAlchemy ORM
Get reviews with python googlemap api
Get AWS account ID with boto3
Get started with MicroPython (on macOS)
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
Get one column from DataFrame with DataFrame
[Python] Get economic data with DataReader
Combining polymorphic inheritance with many-to-many with SQLAlchemy
Introduction to RDB with sqlalchemy II
How to get started with Scrapy
How to get started with Python
DB table insertion process using sqlalchemy