Python - SQLite JSON1 加载扩展

2024-02-21

我想在 Python 中使用 SQLite 的 json1 扩展。根据官方文档 http://sqlite.org/json1.html,它应该是一个可加载的扩展。我从以下位置获取了 json1.c 文件source http://sqlite.org/cgi/src/raw/ext/misc/json1.c?name=9799e4252b305edcbe659329eec3ca80ed85f968并将其编译成 json1.so 按照官方说明 http://sqlite.org/loadext.html#build没有任何错误。

$ gcc -g -fPIC -shared json1.c -o json1.so

当我尝试根据Python 2.7.12(和3.5.2)加载扩展时,出现了问题sqlite3 文档 http://docs.python.org/2/library/sqlite3.html.

>>> import sqlite3
>>> con = sqlite3.connect(":memory:")
>>> con.enable_load_extension(True)
>>> con.load_extension("./json1.so")

我收到以下回溯错误消息。我从包含 json1.so 文件的文件夹中运行 Python 解释器。尽管由于最后一个冒号似乎应该有更多信息,但以下是完整的错误消息。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: error during initialization:

实际上不可能在 Python 中使用 json1 作为可加载扩展吗?是我重新编译 SQLite、pysqlite2 等的唯一选择,如中所述this http://charlesleifer.com/blog/using-the-sqlite-json1-and-fts5-extensions-with-python/查尔斯·莱弗 (Charles Leifer) 的博客文章?

EDIT:

事实证明,我收到错误是因为我的机器已经启用了此扩展和其他扩展。启用已启用的扩展的操作触发了错误。到目前为止,我能访问的所有 Linux 计算机都已经在 Python 附带的 SQLite 中启用了 json1 和 fts5 扩展。您可以通过连接到 SQLite 数据库并运行以下查询来检查使用了哪些编译选项。

PRAGMA compile_options;

你可以使用 python 3 运行 sqlite 。这是在我的 mac 上对我有用的:

首先编译可加载扩展:

curl -O http://sqlite.org/2016/sqlite-src-3140100.zip 

unzip sqlite-src-3140100.zip

gcc -g -fPIC -dynamiclib sqlite-src-3140100/ext/misc/json1.c -o json1

然后在脚本中使用它:

import sqlite3
conn = sqlite3.connect('testingjson.db')

#load precompiled json1 extension
conn.enable_load_extension(True)
conn.load_extension("./json1")

# create a cursor
c = conn.cursor()

# make a table
# create table NAME_OF_TABLE (NAME_OF_FIELD TYPE_OF_FIELD);
c.execute('create table testtabledos (testfield JSON);')

# Insert a row of data into a table
c.execute("insert into testtabledos (testfield) values (json('{\"json1\": \"works\"}'));")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

或在外壳中:

.load json1
CREATE TABLE test_table (id INTEGER, json_field JSON);
# insert data into test table
insert into test_table (id, json_field) values (1, json('{"name":"yvan"}'));
insert into test_table (id, json_field) values (2, json('{"name":"sara"}'));
#select json objects from the json column
select * from test_table where json_extract("json_field", '$.name') is not null;
1|{"name":"yvan"}
2|{"name":"sara"}

我希望这更容易。看起来加载扩展(而不是在创建时将它们构建到 sqlite 中)更有意义。我的最新问题是我似乎无法在 CentOS 6 上编译 json1 扩展。

我在这里写了一个指南:https://github.com/SMAPPNYU/smapphowto/blob/master/howto_get_going_with_sqlite_json1.md https://github.com/SMAPPNYU/smapphowto/blob/master/howto_get_going_with_sqlite_json1.md

编辑:为了我的目的,我最终放弃了 json1 。我现在只使用 pysmap转储到csv https://github.com/SMAPPNYU/pysmap#dump_to_csv通过提取我想要的字段来基于列的 csv,然后转储到sqlite_db https://github.com/SMAPPNYU/pysmap#dump_to_sqlite_db从该 csv 创建一个普通的 sqlite 数据库。看pysmap smapp_collection https://github.com/SMAPPNYU/pysmap#smapp_collection

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python - SQLite JSON1 加载扩展 的相关文章

随机推荐