Skip to content

Sets a basic skeleton that you can use to quickly start interacting with the tables in your predefined database

License

Notifications You must be signed in to change notification settings

mathiasbc/EZAlchemy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EZAlchemy

Quickly interact with your database by loading tables to the class EZAlchemy which also comes with some helpfull methods for inserting and deleting rows a safe way.

Installing:

$ pip install ezalchemy

Usage:

from ezalchemy import EZAlchemy

DB = EZAlchemy(
    db_user='username',
    db_password='pezzword',
    db_hostname='127.0.0.1',
    db_database='mydatabase',
    d_n_d='mysql'   # stands for dialect+driver
)


# This function loads all tables in the database to the class instance DB
DB.connect()

# Or just load tables that you need to use, suppose your table is named "cars"
DB.connect(['cars'])

# insert elements a safe way
car = DB.insert(DB.cars, brand='Audi', year=2009, color='green')
print(car.color)

# query all cars
all_cars = DB.session.query(DB.cars).all()

# query certain columns (use class attributes for columns)
result = DB.session.query(DB.cars).filter(DB.cars.color == 'green')
print([r.brand for r in result])

# same query as above (using string for table columns)
DB.session.query(DB.cars).filter(getattr((DB.cars), 'color') == 'green')
print([r.brand for r in result]) 

# change some attributes
green_car = DB.session.query(DB.cars).filter(DB.cars.color == 'green').first()
green_car.color = 'blue'
blue_car = DB.merge(green_car)
assert blue_car == DB.session.query(DB.cars).filter(DB.cars.color == 'blue').first()

# delete some elements
result = DB.session.query(DB.cars).filter(DB.cars.year < 1980)
DB.delete(result)   # will delete all rows of cars older than 1980

# delete individual element
new_car = DB.insert(DB.cars, brand='VW', year=2016, color='red')
DB.delete(new_car)

# We can also use the "engine" to do raw SQL queries
rows = DB.engine.execute('SELECT * from cars')
for row in rows:
    print(row[0], row[1])

Contributing

Clone the repository, install dependencies and run the tests:

$ git clone https://github.com/mathiasbc/EZAlchemy.git
$ cd EZAlchemy
$ pip install -r dev_requirements.txt
$ py.test

Feel free to make pull requests with any features that you would like to see or bug fixes.

About

Sets a basic skeleton that you can use to quickly start interacting with the tables in your predefined database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages