Ejemplo n.º 1
0
 def setUpClass(self):
     db_name = 'sqlite'
     db_strs = {
         'postgresql': 'postgresql://*****:*****@localhost/sqlpharmacy',
         'sqlite': 'sqlite:///sqlpharmacy/testsuite/sqlpharmacy.db',
         'mysql': 'mysql://*****:*****@localhost/sqlpharmacy?charset=utf8',
     }
     db_str = db_strs[db_name]
     self.db = Database(db_str)
     self.db.drop_tables()
     self.db.create_tables()
     self.db.load_data(fixtures)
Ejemplo n.º 2
0
from sqlalchemy import Column, String

__metaclass__ = Database.DefaultMeta


class User:
    name = Column(String(30))


@Database.many_to_many(User,
                       ref_name='users',
                       backref_name='roles',
                       middle_table_name='user_role')
class Role:
    name = Column(String(30))


Database.register()

if __name__ == '__main__':
    db = Database('sqlite://')
    db.create_tables()

    user1 = User(name='Tyler Long')
    user2 = User(name='Peter Lau')
    role = Role(name='Administrator', users=[user1, user2])
    db.session.add_then_commit(role)

    admin_role = db.session.query(Role).filter_by(name='Administrator').one()
    print(', '.join([user.name for user in admin_role.users]),
          'are administrators')
Ejemplo n.º 3
0
from sqlpharmacy.core import Database
from sqlalchemy import Column, String

__metaclass__ = Database.DefaultMeta

class User:
    name = Column(String(30))

Database.register()

if __name__ == '__main__':
    db = Database('sqlite://') # database urls: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls
    db.create_tables() # create tables, you don't have to write any SQL.

    user = User(name = 'Hello World')
    db.session.add_then_commit(user) # commit user to database.

    user = db.session.query(User).get(1)
    print('My name is', user.name)
    print('created_at', user.created_at) # created_at and updated_at timestamps are added automatically.
    print('updated_at', user.updated_at)

    user.name = 'Tyler Long'
    db.session.commit() # commit changes to database.
    print('My name is', user.name)
    print('created_at', user.created_at)
    print('updated_at', user.updated_at)