从 sqlite3 转储数据时出错

Error while dumping out data from sqlite3

我使用了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)

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

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

请验证您的 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 也能正常工作。