由于BerkeleyDB可以使用SQLite API,那么Python可以使用sqlite模块连接到BerkeleyDB。本文建议使用其他方法,但其可能是在API同步之前编写的。
这里 可以找到一个简单的连接字符串。如果有任何已知问题,请务必发布。我正在探索此主题。
在Linux和Windows上使用Python 2.7。
下载db-5.2.36.tar.gz。
tar xzvf db-5.2.36.tar.gz
cd db-5.2.36/build_unix/
CFLAGS="-fPIC" ../dist/configure --enable-static --disable-shared --enable-sql-compat
# you need -fPIC to build the python ext of pysqlite
make
make prefix=/tmp/bdb install
include_dirs=/tmp/bdb/include
library_dirs=/tmp/bdb/lib
然后在pysqlite中切换目录(cd):
python setup.py build
python setup.py install
或者不需要安装:
cd build/lib.linux-x86_64-2.7
python
from pysqlite2 import dbapi2
conn = dbapi2.connect('test.db')
c = conn.cursor()
c.execute('bla bla bla sql')
在win32上构建和合并库是具有挑战性的 :)
我的假设:
下载bdb和pysqlite(这次我有2.6.3版本),并将其放置在c:\bdb
,解压bdb,以便你拥有:
C:\bdb\db-5.2.36
进入 C:\bdb\db-5.2.36\build_windows
,选择 Berkeley_DB_vs2010.sln
,
选择 Static Release
作为配置并进行构建。
你需要在其中拥有 libdb52s.lib
和 libdb_sql52s.lib
。
C:\bdb\db-5.2.36\build_windows\Win32\Static Release
现在在 c:\bdb
中解压缩 pysqlite,进入 C:\bdb\pysqlite-2.6.3
并按照以下方式编辑 setup.cfg
:
[build_ext]
include_dirs=C:\bdb\db-5.2.36\lang\sql\generated
library_dirs=C:\bdb\db-5.2.36\build_windows\Win32\Static Release
define=SQLITE_OMIT_LOAD_EXTENSION
请务必移除"libraries=",我不得不在setup.py中添加它们,因为我们需要指定多个静态链接库。 如果有人知道如何在setup.cfg中指定列表,请告诉我:)
现在打开setup.py
,转到第191行并替换:
libraries=libraries
使用:
libraries=['libdb_sql52s', 'libdb52s', 'ws2_32', 'advapi32'],
set DISTUTILS_USE_SDK=1
set MSSdk=1
python setup.py build
# ignore errors about manifests, just make sure _sqlite.pyd is built
# make same tests of the linux instructions
python setup.py bdist_wininst
will make the .exe installer in dist subdir