- python 2.7
- virtualenv
- Docker
$ git clone git@github.com:lbenamer/sqla_playground.git sqlapp
$ cd sqlapp
$ bash install.sh
You will find a .env
file at root level to configure sqlapp project
DB_PORT=5532
DB_NAME=sqlapp
PYTHON_BIN=python2.7 # python bin name to create virtual env
DB_URI=postgresql://postgres@localhost:${DB_PORT}/${DB_NAME}
WORK_ENV= path/to/project/folder # add path to python path to be able to use project import into sqlapp
DEPENDENCIES= path/to/requirement.txt # path of python dependencies to add
$ bash launch.sh
$ bash uninstall.sh
from sqlapp import SqlApp
s = SqlApp()
s.connect('postgresql://postgres@localhost:5532/sqlapp')
from sqlalchemy import Column, Integer, String
Base = s.Base
class User(Base):
__tablename__ = 'user'
id = Column(Integer,
primary_key=True,
nullable=False,
)
name = Column(String(), nullable=False)
city = Column(String(), nullable=False)
# Create user table in db
s.create_models(Base)
user = User(name='player', city='paris')
s.create(user)
You also create multiple records with the populate()
method :
ROWS = [
{ 'name': 'Walid', 'city': 'Djerba' },
{ 'name': 'Mathias', 'city': 'Tours' },
]
s.populate(User, ROWS)
player = s.session.query(User).filter_by(name='player').first()
player.city
player = s.session.query(User).filter_by(name='player').first()
s.delete(player)
This inner class contain methods to directly interact with the database
s.sql.tables
# Drop a table
s.sql.drop_table('user')
## Drop all tables
s.sql.drop_all_table()