Last active
August 26, 2018 19:36
-
-
Save mvanorder/a7141b6035661c634304ca1d4d973f0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2018-08-26 15:15:24,041 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1 | |
2018-08-26 15:15:24,066 INFO sqlalchemy.engine.base.Engine () | |
2018-08-26 15:15:24,070 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1 | |
2018-08-26 15:15:24,073 INFO sqlalchemy.engine.base.Engine () | |
2018-08-26 15:15:24,077 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("person") | |
2018-08-26 15:15:24,079 INFO sqlalchemy.engine.base.Engine () | |
2018-08-26 15:15:24,082 INFO sqlalchemy.engine.base.Engine | |
CREATE TABLE person ( | |
id INTEGER NOT NULL, | |
name VARCHAR(30), | |
age INTEGER, | |
PRIMARY KEY (id) | |
) | |
2018-08-26 15:15:24,087 INFO sqlalchemy.engine.base.Engine () | |
2018-08-26 15:15:24,090 INFO sqlalchemy.engine.base.Engine COMMIT | |
>>> dir(Person) | |
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_sa_class_manager', 'age', 'id', 'name'] | |
>>> person = Person() | |
>>> person.name = "John" | |
>>> person.age = 25 | |
>>> session.add(person) | |
>>> print(person) | |
<__main__.Person object at 0x0000016B04B13AC8> | |
>>> print(person.name) | |
John | |
>>> session.flush() | |
2018-08-26 15:17:55,436 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) | |
2018-08-26 15:17:55,463 INFO sqlalchemy.engine.base.Engine INSERT INTO person (name, age) VALUES (?, ?) | |
2018-08-26 15:17:55,474 INFO sqlalchemy.engine.base.Engine ('John', 25) | |
>>> session.commit() | |
2018-08-26 15:18:04,115 INFO sqlalchemy.engine.base.Engine COMMIT | |
>>> Person.id | |
<sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x0000016B04AAFD58> | |
>>> print(person.id) | |
2018-08-26 15:18:34,633 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) | |
2018-08-26 15:18:34,682 INFO sqlalchemy.engine.base.Engine SELECT person.id AS person_id, person.name AS person_name, person.age AS person_age | |
FROM person | |
WHERE person.id = ? | |
2018-08-26 15:18:34,694 INFO sqlalchemy.engine.base.Engine (1,) | |
1 | |
>>> p = session.query(Person) | |
>>> p | |
<sqlalchemy.orm.query.Query object at 0x0000016B04B2B6D8> | |
>>> p[0] | |
2018-08-26 15:19:19,269 INFO sqlalchemy.engine.base.Engine SELECT person.id AS person_id, person.name AS person_name, person.age AS person_age | |
FROM person | |
LIMIT ? OFFSET ? | |
2018-08-26 15:19:19,281 INFO sqlalchemy.engine.base.Engine (1, 0) | |
<__main__.Person object at 0x0000016B04B13AC8> | |
>>> p[0].name | |
2018-08-26 15:19:31,841 INFO sqlalchemy.engine.base.Engine SELECT person.id AS person_id, person.name AS person_name, person.age AS person_age | |
FROM person | |
LIMIT ? OFFSET ? | |
2018-08-26 15:19:31,897 INFO sqlalchemy.engine.base.Engine (1, 0) | |
'John' | |
>>> p2 = session(Person, id=1) | |
Traceback (most recent call last): | |
File "<pyshell#15>", line 1, in <module> | |
p2 = session(Person, id=1) | |
TypeError: __call__() takes 1 positional argument but 2 were given | |
>>> p0_name = p[0].name | |
2018-08-26 15:21:03,859 INFO sqlalchemy.engine.base.Engine SELECT person.id AS person_id, person.name AS person_name, person.age AS person_age | |
FROM person | |
LIMIT ? OFFSET ? | |
2018-08-26 15:21:03,879 INFO sqlalchemy.engine.base.Engine (1, 0) | |
>>> p0_name | |
'John' | |
>>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import orm | |
from sqlalchemy import schema, types | |
from sqlalchemy import create_engine | |
metadata = schema.MetaData() | |
person_table = schema.Table( | |
'person', metadata, | |
schema.Column('id', types.Integer, | |
schema.Sequence('page_seq_id', optional=True), primary_key=True), | |
schema.Column('name', types.Unicode(30)), | |
schema.Column('age', types.Integer), | |
) | |
class Person(object): | |
pass | |
orm.mapper(Person, person_table) | |
engine = create_engine('sqlite:///:memory:', echo=True) | |
metadata.bind = engine | |
metadata.create_all() | |
# Set up the session | |
sm = orm.sessionmaker(bind=engine, autoflush=True, autocommit=False, | |
expire_on_commit=True) | |
session = orm.scoped_session(sm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment