为什么pysqlite无法正常工作?
我尝试安装pysqlite。在安装过程中出现了一些可疑的情况。当我输入:
python setup.py build
最后我得到了以下信息:
src/module.c:286: error: ‘SQLITE_PRAGMA’ undeclared here (not in a function)
src/module.c:287: error: ‘SQLITE_READ’ undeclared here (not in a function)
src/module.c:288: error: ‘SQLITE_SELECT’ undeclared here (not in a function)
src/module.c:289: error: ‘SQLITE_TRANSACTION’ undeclared here (not in a function)
src/module.c:290: error: ‘SQLITE_UPDATE’ undeclared here (not in a function)
src/module.c:291: error: ‘SQLITE_ATTACH’ undeclared here (not in a function)
src/module.c:292: error: ‘SQLITE_DETACH’ undeclared here (not in a function)
src/module.c: In function ‘init_sqlite’:
src/module.c:419: warning: implicit declaration of function ‘sqlite3_libversion’
src/module.c:419: warning: passing argument 1 of ‘PyString_FromString’ makes pointer from integer without a cast
error: command 'gcc' failed with exit status 1
我忽略了最后一行,决定继续。所以,我输入了:
python setup.py install
然后,我又得到了类似的错误信息:
src/module.c:288: error: ‘SQLITE_SELECT’ undeclared here (not in a function)
src/module.c:289: error: ‘SQLITE_TRANSACTION’ undeclared here (not in a function)
src/module.c:290: error: ‘SQLITE_UPDATE’ undeclared here (not in a function)
src/module.c:291: error: ‘SQLITE_ATTACH’ undeclared here (not in a function)
src/module.c:292: error: ‘SQLITE_DETACH’ undeclared here (not in a function)
src/module.c: In function ‘init_sqlite’:
src/module.c:419: warning: implicit declaration of function ‘sqlite3_libversion’
src/module.c:419: warning: passing argument 1 of ‘PyString_FromString’ makes pointer from integer without a cast
error: command 'gcc' failed with exit status 1
之后我想看看pysqlite是否能正常工作。如果在Python命令行模式下我输入
from pysqlite2 import *
Python没有报错。但是,如果我尝试按照书中的例子:
from pysqlite2 import dbapi2 as sqlite
我就收到了一个错误信息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pysqlite2/dbapi2.py", line 27, in <module>
from pysqlite2._sqlite import *
ImportError: No module named _sqlite
有没有人知道为什么会这样,以及这个问题怎么解决。顺便说一下,我安装了一个新的Python版本。“python -V”显示的是“Python 2.6.2”。感谢任何帮助。
3 个回答
2
现在,构建 pysqlite 的正确方法已经在网站上发布了。
$ tar xvfz <version>.tar.gz
$ cd <version>
$ python setup.py build_static install
build_static 这个命令会下载最新的 sqlite 代码,并进行静态编译。顺便提一下,我刚刚在一个 1and1 的共享主机上做了这个。
3
我就忽略了最后一行,决定继续。
你不能就这样忽略最后一行。它告诉你有个错误,所以程序无法编译。接下来你运行的命令告诉你无法安装,因为它无法编译。然后,你的Python告诉你无法运行代码,因为它没有安装。你需要先解决编译的问题,才能继续安装。
2
需要学习一下如何编译Python扩展,你现在用的是什么操作系统?看起来你缺少sqlite的头文件,这些文件是用来定义一些宏的。当你运行Python的设置时,它会编译与sqlite的本地二进制文件的连接,并把一些.py文件复制到库里。通常情况下,_sqlite是一个.pyd文件(相当于Windows中的dll文件),里面有调用sqlite库的代码,但在你的情况下,这个文件没有被生成。
检查一下sqlite的头文件等是否存在。