1 from sqlalchemy
import MetaData
, Table
, create_engine
, orm
3 from .tables
import metadata
6 """Connects to the requested URI. Returns a session object.
8 Calling this function also binds the metadata object to the created engine.
11 ### Do some fixery for MySQL
12 if uri
[0:5] == 'mysql':
13 # MySQL uses latin1 for connections by default even if the server is
14 # otherwise oozing with utf8; charset fixes this
15 if 'charset' not in uri
:
16 uri
+= '?charset=utf8'
18 # Tables should be InnoDB, in the event that we're creating them
19 for table
in metadata
.tables
.values():
20 table
.kwargs
['mysql_engine'] = 'InnoDB'
23 engine
= create_engine(uri
)
24 conn
= engine
.connect()
25 metadata
.bind
= engine
27 sm
= orm
.sessionmaker(autoflush
=True, autocommit
=False, bind
=engine
)
28 session
= orm
.scoped_session(sm
)