[PYTHON] How to read dynamically generated table definitions using SQLAlchemy

How to read dynamically generated table definitions using SQLAlchemy

I want to do the title! I looked it up, so I made a note. In conclusion, it's OK if you create a mapping object using the built-in function type. For example, write like this.

engine = create_engine(Database URL, echo=True, encoding='utf-8')
base = declarative_base(engine)

dic = {
    '__tablename__':table name,
    '__table_args__': {'autoload': True}}}
obj = type('MyObject', (base,), dic)

The point is to set autoload = True to create a mapping from an existing table.

sample

Sample code

sql_sample.py


# -*- coding: utf-8 -*-
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

schema = 'sqlite:///test.db'

engine = create_engine(schema, echo=False, encoding='utf-8')
base = declarative_base(engine)
session = sessionmaker(bind=engine)()

management_dic = {
    '__tablename__': 'management',
    '__table_args__': {'autoload': True}}
management_object = type('management_object', (base,), management_dic)
fetch_management = session.query(management_object).all()

for management_row_object in fetch_management:
    folder_table_dic = {
        '__tablename__': management_row_object.folder_table_name,
        '__table_args__': {'autoload': True}}
    folder_object_name = management_row_object.folder_table_name.encode('utf-8')
    folder_object = type(folder_object_name, (base,), folder_table_dic)
    fetch_folder = session.query(folder_object).all()

    print "-" * 32
    print "table_name: %s" % folder_object_name
    print "-" * 32
    for row in fetch_folder:
        print "  fid: %s,  file_name: %s" % (row.fid, row.file_name)
    print

Database settings to use the sample code

A table that manages dynamically created tables (management)

tid folder_table_id
0 folder_0000
1 folder_0001

management.sql


CREATE TABLE management(
    tid INTEGER NOT NULL PRIMARY KEY,
    folder_table_name TEXT NOT NULL);
INSERT INTO management(tid, folder_table_name) VALUES (0,'folder_0000');
INSERT INTO management(tid, folder_table_name) VALUES (1,'folder_0001');

Dynamically generated table (folder_0000)

fid file_name
0 ham.txt
1 spam.txt
2 egg.txt

folder_0000.sql


CREATE TABLE folder_0000(
    fid INTEGER NOT NULL PRIMARY KEY,
    file_name TEXT NOT NULL
);
INSERT INTO folder_0000(fid, file_name) VALUES (0,'ham.txt');
INSERT INTO folder_0000(fid, file_name) VALUES (1,'spam.txt');
INSERT INTO folder_0000(fid, file_name) VALUES (2,'egg.txt');

Dynamically generated table (folder_0001)

fid file_name
0 foo.txt
1 bar.txt

folder_0001.sql


CREATE TABLE folder_0001(
    fid INTEGER NOT NULL PRIMARY KEY,
    file_name TEXT NOT NULL
);
INSERT INTO folder_0001(fid, file_name) VALUES (0,'foo.txt');
INSERT INTO folder_0001(fid, file_name) VALUES (1,'bar.txt');

Run the sample

sqlite3 test.db < management.sql
sqlite3 test.db < folder_0000.sql
sqlite3 test.db < folder_0001.sql
python sql_sample.py

Sample execution result

--------------------------------
table_name: folder_0000
--------------------------------
  fid: 0,  file_name: ham.txt
  fid: 1,  file_name: spam.txt
  fid: 2,  file_name: egg.txt

--------------------------------
table_name: folder_0001
--------------------------------
  fid: 0,  file_name: foo.txt
  fid: 1,  file_name: bar.txt

reference

Python> Documentation> Python standard library> 2. Built-in functions Dynamically setting _tablename_ for sharding in SQLAlchemy?

Recommended Posts

How to read dynamically generated table definitions using SQLAlchemy
How to read PyPI
How to read JSON
Get table dynamically with sqlalchemy
How to update with SQLAlchemy?
How to Alter with SQLAlchemy?
How to Delete with SQLAlchemy?
How to install python using anaconda
How to handle session in SQLAlchemy
How to read e-Stat subregion data
How to read the SNLI dataset
DB table insertion process using sqlalchemy
How to format a table using Pandas apply, pivot and swaplevel
How to INNER JOIN with SQLAlchemy
Connect to MySQL using Flask SQLAlchemy
How to get parent id with sqlalchemy
How to read pydoc on python interpreter
How to dynamically define variables in Python
How to draw a graph using Matplotlib
How to set up SVM using Optuna
How to install a package using a repository
How to set xg boost using Optuna
How to display multiplication table in python
How to dynamically zero pad in Python
How to download youtube videos using pytube3
How to read CSV files in Pandas
How to read problem data with paiza
How to use SQLAlchemy / Connect with aiomysql
How to display Map using Google Map API (Android)
How to read a CSV file with Python 2/3
Dynamically generate sqlalchemy filter according to the conditions
How to code a drone using image recognition
How to set up Random forest using Optuna
[Python] How to read excel file with pandas
[Python] How to read data from CIFAR-10 and CIFAR-100
How to deal with SessionNotCreatedException when using Selenium
How to read an array with Python's ConfigParser
How to get article data using Qiita API
How to set up Random forest using Optuna
I read "How to make a hacking lab"
How to read time series data in PyTorch
How to search HTML data using Beautiful Soup
Tensorflow, Tensorflow After all, which one (How to read Tensorflow)
How to upload to a shared drive using pydrive
How to uninstall a module installed using setup.py
How to read a file in a different directory