数据库:Embedded FireBird 2.0 beta 3
驱动:KInterbasDB 3.2 for Win
1、将下载的KInterbasDB 驱动解压,运行其.exe安装程序,默认会安装到Python 的“lib\site-packages\kinterbasdb”目录下。
2、在上面提到的“kinterbasdb”目录下创建一个目录“embedded”,然后解压下载来的Embedded FireBird,复制“fbembed.dll”、“firebird.msg”、“ib_util.dll”3个文件到“embedded”目录下——如果需要用到ASCII 外的字符集的话,同时复制“intl”子目录(里面包含“fbintl.dll”文件)到“embedded”目录下)。
3、将“embedded”目录下的“fbembed.dll”文件改名为“fbclient.dll”。
最终的目录结构如下:
Python 的安装目录\
Lib\
site-packages\
kinterbasdb\
embedded\
fbclient.dll
firebird.msg
ib_util.dll
intl\ //如果需要的话
fbintl.dll
4、完成,运行KInterbasDB-Base 的Python 程序就可以连接目标数据库了。
5、Example
import datetime, decimal, os.path, string, sys
import kinterbasdb
kinterbasdb.init(type_conv=200) #注[1]
# This program never imports mx.DateTime:
assert 'mx' not in sys.modules
def test():
dbFilename = r'D:\temp\test-deferred.fdb'
prepareTestDatabase(dbFilename)
# Connect with character set UNICODE_FSS, to match the default character
# set of the test database.
con = kinterbasdb.connect(dsn=dbFilename,
user='sysdba', password='masterkey', charset='UNICODE_FSS'
)
cur = con.cursor()
# Create a test table.
cur.execute("""
create table test (
a numeric(18,2),
b date,
c time,
d timestamp,
e varchar(50), /* Defaults to character set UNICODE_FSS. */
f varchar(50), /* Defaults to character set UNICODE_FSS. */
g varchar(50) character set ASCII
)
""")
con.commit()
# Create an input value for each field in the test table.
aIn = decimal.Decimal('4.53')
# Notice that the DB API date/time constructors in kinterbasdb generate
# datetime-based objects instead of mx-based objects because of our earlier
# call to kinterbasdb.init(type_conv=200).
bIn = kinterbasdb.Date(2004,1,4)
assert isinstance(bIn, datetime.date)
cIn = kinterbasdb.Time(16,27,59)
assert isinstance(cIn, datetime.time)
dIn = kinterbasdb.Timestamp(2004,1,4, 16,27,59)
assert isinstance(dIn, datetime.datetime)
eIn = u'A unicod\u2211 object stored in a Unicode field.'
fIn = 'A str object stored in a Unicode field.'
gIn = 'A str object stored in an ASCII field.'
print '-' * 70
inputValues = (aIn, bIn, cIn, dIn, eIn, fIn, gIn)
reportValues('In', inputValues)
cur.execute("insert into test values (?,?,?,?,?,?,?)", inputValues)
print '-' * 70
cur.execute("select a,b,c,d,e,f,g from test")
(aOut, bOut, cOut, dOut, eOut, fOut, gOut) = outputValues = cur.fetchone()
reportValues('Out', outputValues)
print '-' * 70
# Notice that all values made the journey to and from the database intact.
assert inputValues == outputValues
def reportValues(direction, values):
for (val, c) in zip(values, string.ascii_lowercase[:len(values)]):
varName = c + direction
print '%s has type %s, value\n %s' % (varName, type(val), repr(val))
def prepareTestDatabase(dbFilename):
# Delete the test database if an old copy is already present.
if os.path.isfile(dbFilename):
conOld = kinterbasdb.connect(dsn=dbFilename,
user='sysdba', password='masterkey'
)
conOld.drop_database()
# Create the test database afresh.
kinterbasdb.create_database("""
create database '%s'
user 'sysdba' password 'masterkey'
default character set UNICODE_FSS
""" % dbFilename
)
if __name__ == '__main__':
test()
注[1]:
KInterbasDB 为了向后兼容,默认情况下是使用“mx.DateTime”模块,但“mx.DateTime”被明确地表示是不必的!在Python 标准库里的“datetime”模块会更容易使用,完全可以代替“mx.DateTime”!如果用“datetime”代替默认的“mx.DateTime”,只需要简单地修改代码:
将
“import kinterbasdb”
替换为
“import kinterbasdb; kinterbasdb.init(type_conv=200)”
有一点值得提下,如果你用的Python 版本是低于2.4的话,你还是需要“mx.DateTime”模块的,可以到以下网址下载:http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html
没有评论:
发表评论