Custom DBAPI connect() arguments
Custom arguments used when issuing the connect()
call to the underlyingDBAPI may be issued in three distinct ways. String-based arguments can bepassed directly from the URL string as query arguments:(第一种方式URL query string:加上"?charset=utf8")
db = create_engine('postgresql://scott:tiger@localhost/test?argument1=foo&argument2=bar')
If SQLAlchemy’s database connector is aware of a particular query argument, itmay convert its type from string to its proper type.
create_engine()
also takes an argument connect_args
which is an additional dictionary that will be passed to connect()
. This can be used when arguments of a type other than string are required, and SQLAlchemy’s database connector has no type conversion logic present for that parameter:(第二种方式使用connect_arg参数方式)
db = create_engine('postgresql://scott:tiger@localhost/test', connect_args = {'argument1':17, 'argument2':'bar'})
The most customizable connection method of all is to pass a creator
argument, which specifies a callable that returns a DBAPI connection:
def connect():
return psycopg.connect(user='scott', host='localhost')
db = create_engine('postgresql://', creator=connect)