使用SQLObject连接数据库
任何使用sqlobject的程序都需要导入sqlobject包。
from sqlobject import *
连接数据库
连接URI必须遵循标准URI语法:
scheme://[user[:password]@]host[:port]/database[?parameters]
SQLObject支持sqlite、mysql、postgres、firebird、interbase、maxdb、sapdb、mssql、sybase。
在连接vastbase数据库时,使用连接postgres的模式。连接URL如:
postgres://user@host/database?debug=&cache= postgres:///full/path/to/socket/database postgres://host:5432/database
参数包括:
debug(默认值:False)。
debugOutput(默认值:False)。
cache(默认值:True)。
autoCommit(默认值:True)。
debugThreading(默认值:False)。
logger(默认值:None)。
loglevel(默认值:None)。
schema(默认值:None)。
建立连接如下:
from sqlobject import * connection_string = 'postgres://vbadmin:vast_123@127.0.0.1:5432/vastbase' connection = connectionForURI(connection_string) sqlhub.processConnection = connection
sqlhub.processConnection意味着在默认情况下,所有类都将使用用户在如上建立的连接。如果没有定义sqlhub,则需要使用
Person._connection=conn
来指定连接。