[PYTHON] Using Sessions and Reflections with SQLAlchemy

I wanted to process the already generated table, and when I tried to manage the session of the reflected table with SQLAlchemy, it got strange, so I summarized it. In the end, all you have to do is use automap_base ().

I don't understand scralchemy at all, so I welcome you to point out.

Prerequisites

--mac (OS X El Capitan version 10.11.5)

Source code example

session.py


# -*- coding: utf-8 -*-
import sys
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import sessionmaker

def main(sys_argv):
    # MAMP + Mysql-How to generate an engine in Python environment
    engine = sqlalchemy.create_engine(
        "mysql://user_name:password@localhost/db_name"
        + "?unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock",
        echo=False)

    #Session start
    session = sessionmaker(bind=engine)()

    #Generate a mapped instance using reflection
    base = automap_base()
    base.prepare(engine, reflect=True)

    #Do what you want to do
    # your_table delete from table_If you want to display id with flag 0
    your_table = base.classes.your_table
    result = session.query(your_table).filter(delete_flag == 0)
    for row in result:
        print row.id   

    #End of session
    session.close()

if __name__ == '__main__':
    main(sys.argv)

Recommended Posts

Using Sessions and Reflections with SQLAlchemy
group_by with sqlalchemy and sum
Using Python and MeCab with Azure Databricks
I'm using tox and Python 3.3 with Travis-CI
Use SQLAlchemy and multiprocessing
Use Enums with SQLAlchemy
With and without WSGI
IP spoof using tor on macOS and check with python
Using Python with SPSS Modeler extension nodes ① Setup and visualization
Try MD simulation with ANN potential using aenet and ASE
Serial communication control with python and I2C communication (using USBGPIO8 device)
Using MLflow with Databricks ② --Visualization of experimental parameters and metrics -
Serial communication control with python and SPI communication (using USBGPIO8 device)
This and that for using Step Functions with CDK + Python
With me, cp, and Subprocess
Using X11 with ubuntu18.04 (C)
Programming with Python and Tkinter
Get table dynamically with sqlalchemy
When using optparse with iPython
Working with tkinter and mouse
Using Graphviz with Jupyter Notebook
Python and hardware-Using RS232C with Python-
[S3] CRUD with S3 using Python [Python]
Messaging with AMQP using kombu
Using Quaternion with Python ~ numpy-quaternion ~
Try using matplotlib with PyCharm
Use DATE_FORMAT with SQLAlchemy filter
Games using IMU with SenseHat
[Python] Using OpenCV with Python (Basic)
Introduction to RDB with sqlalchemy Ⅰ
How to update with SQLAlchemy?
Browse columns encrypted with sqlalchemy
Try using SQLAlchemy + MySQL (Part 1)
Try using SQLAlchemy + MySQL (Part 2)
Using a printer with Debian 10
python metaclass and sqlalchemy declareative
When using if and when using while
How to Alter with SQLAlchemy?
This and that using reflect
Try using pytest-Overview and Samples-
Super-resolution with SRGAN and ESRGAN
python with pyenv and venv
Support multi session with SQLAlchemy
How to Delete with SQLAlchemy?
With me, NER and Flair
Try using folium with anaconda
Using OpenCV with Python @Mac
Works with Python and R
Send using Python with Gmail
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
I tried using PyEZ and JSNAPy. Part 4: Automate ISP setup with PyEZ and JSNAPy
Addicted to character code by inserting and extracting data with SQLAlchemy