<p>To start, heres the tables we will work with again:</p>
<&|formatting.myt:code&>
- from sqlalchemy.schema import *
- import sqlalchemy.engine as engine
- db = engine.create_engine('sqlite', 'mydb', {}, echo=True)
+ from sqlalchemy import *
+ db = create_engine('sqlite', {'filename':'mydb'}, echo=True)
# a table to store users
users = Table('users', db,
</p>
<&|doclib.myt:item, name="example", description="Basic Example" &>
<&|formatting.myt:code&>
- from sqlalchemy.schema import *
- from sqlalchemy.mapper import *
+ from sqlalchemy import *
# table metadata
users = Table('users', engine,
<&|doclib.myt:item, name="relations", description="Defining and Using Relationships" &>
<p>So that covers how to map the columns in a table to an object, how to load objects, create new ones, and save changes. The next step is how to define an object's relationships to other database-persisted objects. This is done via the <span class="codeline">relation</span> function provided by the mapper module. So with our User class, lets also define the User has having one or more mailing addresses. First, the table metadata:</p>
<&|formatting.myt:code&>
- from sqlalchemy.schema import *
- from sqlalchemy.mapper import *
- import sqlalchemy.databases.sqlite as sqlite
- engine = sqlite.engine('mydb', {})
+ from sqlalchemy import *
+ engine = create_engine('sqlite', {'filename':'mydb'})
# define user table
users = Table('users', engine,
<p>An example of connecting to each engine is as follows:</p>
<&|formatting.myt:code&>
- import sqlalchemy.engine as engine
+ from sqlalchemy.engine import *
# sqlite in memory
- sqlite_engine = engine.create_engine('sqlite', {'filename':':memory:'}, **opts)
+ sqlite_engine = create_engine('sqlite', {'filename':':memory:'}, **opts)
# sqlite using a file
- sqlite_engine = engine.create_engine('sqlite', {'filename':'querytest.db'}, **opts)
+ sqlite_engine = create_engine('sqlite', {'filename':'querytest.db'}, **opts)
# postgres
- postgres_engine = engine.create_engine('postgres',
+ postgres_engine = create_engine('postgres',
{'database':'test',
'host':'127.0.0.1',
'user':'scott',
'password':'tiger'}, **opts)
# oracle
- oracle_engine = engine.create_engine('oracle',
+ oracle_engine = create_engine('oracle',
{'dsn':'mydsn',
'user':'scott',
'password':'tiger'}, **opts)
</&>
<p>Note that the general form of connecting to an engine is:</p>
<&|formatting.myt:code&>
- engine = sqlalchemy.engine.create_engine(
+ engine = create_engine(
<enginename>,
{<named DBAPI arguments>},
<sqlalchemy options>
)
</&>
<p>The second argument is a dictionary whose key/value pairs will be passed to the underlying DBAPI connect() method as keyword arguments. Any keyword argument supported by the DBAPI module can be in this dictionary.</p>
+ <p>An additional URL-string based calling style will also be added soon, as this is a highly requested feature.
+ </p>
</&>
<&|doclib.myt:item, name="options", description="Database Engine Options" &>
<p>The remaining arguments to <span class="codeline">create_engine</span> are keyword arguments that are passed to the specific subclass of <span class="codeline">sqlalchemy.engine.SQLEngine</span> being used, as well as the underlying <span class="codeline">sqlalchemy.pool.Pool</span> instance. All of the options described in the previous section <&formatting.myt:link, path="pooling_configuration"&> can be specified, as well as engine-specific options:</p>
<p>The core of SQLAlchemy's query and object mapping operations is table metadata, which are Python objects that describe tables. Metadata objects can be created by explicitly naming the table and all its properties, using the Table, Column, ForeignKey, and Sequence objects imported from <span class="codeline">sqlalchemy.schema</span>, and a database engine constructed as described in the previous section, or they can be automatically pulled from an existing database schema. First, the explicit version: </p>
<&|formatting.myt:code&>
from sqlalchemy.schema import *
- import sqlalchemy.engine as engine
- engine = engine.create_engine('sqlite', ':memory:', {}, **opts)
+ engine = create_engine('sqlite', {'filename':':memory:'}, **opts)
users = Table('users', engine,
Column('user_id', Integer, primary_key = True),
<&|formatting.myt:code&>
# create two engines
- sqlite_engine = engine.create_engine('sqlite', 'querytest.db', {})
- postgres_engine = engine.create_engine('postgres',
+ sqlite_engine = create_engine('sqlite', {'filename':'querytest.db'})
+ postgres_engine = create_engine('postgres',
{'database':'test',
'host':'127.0.0.1', 'user':'scott', 'password':'tiger'})
Column('user_name', String(50))
)
- sqlite_engine = engine.create_engine('sqlite', 'querytest.db', {})
+ sqlite_engine = create_engine('sqlite', {'filename':'querytest.db'})
sqlite_users = users.toengine(sqlite_engine)
sqlite_users.create()
</&>
<p>For this section, we will assume the following tables:
<&|formatting.myt:code&>
- from sqlalchemy.schema import *
- import sqlalchemy.engine as engine
- db = engine.create_engine('sqlite', 'mydb', {}, echo=True)
+ from sqlalchemy import *
+ db = create_engine('sqlite', {'filename':'mydb'}, echo=True)
# a table to store users
users = Table('users', db,
<&|doclib.myt:item, name="select", description="Simple Select" &>
<p>A select is done by constructing a <span class="codeline">Select</span> object with the proper arguments, adding any extra arguments if desired, then calling its <span class="codeline">execute()</span> method.
<&|formatting.myt:code&>
- from sqlalchemy.sql import *
+ from sqlalchemy import *
# use the select() function defined in the sql package
s = select([users])