Lesen dynamisch generierter Tabellendefinitionen mit SQL Alchemy
Ich möchte den Titel machen! Ich habe es nachgeschlagen und mir eine Notiz gemacht. Zusammenfassend ist es in Ordnung, wenn Sie ein Zuordnungsobjekt mit dem integrierten Funktionstyp erstellen. Schreiben Sie zum Beispiel so.
engine = create_engine(Datenbank-URL, echo=True, encoding='utf-8')
base = declarative_base(engine)
dic = {
'__tablename__':Tabellenname,
'__table_args__': {'autoload': True}}}
obj = type('MyObject', (base,), dic)
Der Punkt ist, `` `autoload = True``` zu setzen, um eine Zuordnung aus einer vorhandenen Tabelle zu erstellen.
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
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');
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');
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');
sqlite3 test.db < management.sql
sqlite3 test.db < folder_0000.sql
sqlite3 test.db < folder_0001.sql
python sql_sample.py
--------------------------------
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
Python> Dokumentation> Python-Standardbibliothek> 2. Integrierte Funktionen Dynamically setting _tablename_ for sharding in SQLAlchemy?
Recommended Posts