On Nov 24, 2008, at 11:47 PM, Harish Vishwanath wrote:
> Hello,
>
> Thanks for your reply. Please see the below implementation :
>
> Implementation 1:
>
> >>> def my_con_func():
> ... print "My Connection func"
> ... import sqlite3.dbapi2 as sqlite
> ... con = sqlite.connect(":memory:")
> ... con.text_factory=str
> ... return con
> ...
> >>> engine = create_engine("sqlite:///",creator=my_con_func)
> >>> engine
> Engine(sqlite:///)
> >>> engine.connect()
> My Connection func
> <sqlalchemy.engine.base.Connection object at 0x01B1ADB0>
> >>> engine.connect().connection.connection.text_factory
> <type 'str'>
>
>
> Implementation 2:
>
> >>> anotherengine = create_engine("sqlite:///:memory:")
> >>> anotherengine.connect().connection.connection.text_factory = str
>
> I would like to know which one is better, since I am afraid if I am
> missing some create_engine inbuilt implementation while returning
> custom connection object (in Implementation 1). Please let me know
> your suggestion.
go with this one:
from sqlalchemy.interfaces import PoolListener
class SetTextFactory(PoolListener):
def connect(self, dbapi_con, con_record):
dbapi_con.text_factory = str
engine = create_engine('sqlite://', listeners=[SetTextFactory()])
implementation 1 is fine, you can stay with it if you want - I just
think "creator" is a little klunky since you have to re-import sqlite
and connect manually. implementation 2 will only work for a single-
threaded application and will break if the pool implementation is
changed, unless you set-up text_factory every time connect() is called
(which is usually impossible).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---