从sqlite3

时间:2015-08-29 11:25:46

标签: python sqlite pyinstaller

我使用 sqlite3_connection.iterdump()方法将sqlite3转储到数据库中。

我在python中编写了一个转储sqlite3表的模块。如果我在我的机器中本地运行它,该模块工作正常。

并且,在使用pyinstaller创建模块的python包之后,如果我尝试转储数据库,则会出错提示

"ImportError: No module named sqlite3.dump"

知道如何解决这个问题。或者是否有任何替代方法来获取sqlite3转储。

以下是我要转储数据库的内容。

#Export database
def export_database(self):
    database_string = ""
    for line in self.conn.iterdump():
            database_string += '%s\n' % (line)
    return database_string

#Import database
def import_database(self, database_string):
    self.cursor.executescript(database_string)

2 个答案:

答案 0 :(得分:1)

请确认您的 PyInstaller 安装目录下有hooks/hook-sqlite3.py个文件。如果您没有,请安装latest PyInstaller version

如果您无法安装最新版本,请使用以下内容创建文件hook-sqlite3.py

from PyInstaller.hooks.hookutils import collect_submodules
hiddenimports = collect_submodules('sqlite3')

构建时,提供将钩子文件作为值放入--additional-hooks-dir参数的目录的路径,如下所示:

--additional-hooks-dir=<path_to_directory_of_hook_file>

根据下面的评论,似乎在构建过程中添加--hidden-import=sqlite3

答案 1 :(得分:0)

无论您在何处部署模块,都需要先安装sqlite3模块。 最有可能在您的本地环境中,该模块在通用python库中可用。尝试使用virtualenv来避免此类问题,您可以使用pip来安装所有模块的要求。

Sqlite应该包含在你的Python安装中,但这完全取决于源的安装,版本或Python的安装方式。有关详细信息,请查看此处:How can I install sqlite3 to Python?

相关问题