Python - SQLite JSON1负载扩展

Ali*_*hel 9 python sqlite sqlite-json1

我想在Python中使用SQLite的json1扩展.根据官方文档,它应该是可加载的扩展.我从源代码获得了json1.c文件,并按照官方说明将其编译成json1.so,没有任何错误.

$ gcc -g -fPIC -shared json1.c -o json1.so
Run Code Online (Sandbox Code Playgroud)

当我尝试根据sqlite3文档在Python 2.7.12(和3.5.2)中加载扩展时出现问题.

>>> import sqlite3
>>> con = sqlite3.connect(":memory:")
>>> con.enable_load_extension(True)
>>> con.load_extension("./json1.so")
Run Code Online (Sandbox Code Playgroud)

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

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

实际上不可能在Python中使用json1作为可加载扩展吗?我是唯一一个重新编译SQLite,pysqlite2等的选项,正如Charles Leifer 在这篇博客文章中所解释的那样?

编辑:

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

PRAGMA compile_options;
Run Code Online (Sandbox Code Playgroud)

yva*_*her 5

您可以使用 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
Run Code Online (Sandbox Code Playgroud)

然后在脚本中使用它:

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()
Run Code Online (Sandbox Code Playgroud)

或在外壳中:

.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"}
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:为了我的目的,我最终放弃了 json1。我现在只是通过提取我想要的字段来使用 pysmap dump_to_csv到基于列的 csv,然后dump_to_sqlite_db从该 csv 创建一个普通的 sqlite db。见pysmap smapp_collection

  • 它是这样的:`/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/crt1.o:在函数'_start'中: (.text+0x20):对“main”collect2 的未定义引用:错误:ls 返回 1 个退出状态`。但是,我看到其他人遇到了我的问题,但他们通过发现它已经加载并启用来解决它。如果通过 `pragma compile_options` 发现并尝试一下,那么 json1 已经加载并为我启用了!对不起,双重评论。 (3认同)