Ross Wan's World!

Python, Ajax, PHP and Linux.

Python 连接FireBird 嵌入式(Embedded)数据库

Posted by Ross Wan 于 2007/08/02

数据库: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”目录下)。



Python 的安装目录\
            intl\            //如果需要的话


4、完成,运行KInterbasDB-Base 的Python 程序就可以连接目标数据库了。


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’

    # 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.
        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

    # 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,
    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’
    # Create the test database afresh.
        create database ‘%s’
        user ‘sysdba’ password ‘masterkey’
        default character set UNICODE_FSS
        “”” % dbFilename

if __name__ == ‘__main__’:

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”模块的,可以到以下网址下载:



Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: