您的位置:首页 > 编程语言 > Python开发

Python ORM SQLAlchemy 的中文乱码问题解决

2016-03-07 12:58 1561 查看

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: