Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-SQLite JSON1加载扩展_Python_Sqlite_Sqlite Json1 - Fatal编程技术网

Python-SQLite JSON1加载扩展

Python-SQLite JSON1加载扩展,python,sqlite,sqlite-json1,Python,Sqlite,Sqlite Json1,我想在Python中使用SQLite的json1扩展。根据,它应该是一个可加载的扩展。我从中获得了json1.c文件,并将其编译成json1.so,按照,没有任何错误 $ gcc -g -fPIC -shared json1.c -o json1.so 当我试图根据脚本在Python2.7.12(和3.5.2)中加载扩展时,出现了问题 我收到了以下回溯错误消息。我从包含json1.so文件的文件夹中运行Python解释器。尽管由于最后一个冒号,似乎应该有更多的信息,但下面是完整的错误消息 Tr

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

$ gcc -g -fPIC -shared json1.c -o json1.so
当我试图根据脚本在Python2.7.12(和3.5.2)中加载扩展时,出现了问题

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

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

您可以使用Python3运行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扩展

我在这里写了一本指南:


编辑:为了我的目的,我最终放弃了json1。现在,通过提取所需的字段,我只需使用pysmap来创建基于列的csv,然后从该csv创建一个普通的sqlite数据库。请参见

,了解如何从源代码构建json1扩展,如下所示:

从下载后,将cd解压缩到其文件夹中,然后运行
/configure

然后将以下内容添加到生成的
Makefile

json1.dylib: json1.lo  
    $(LTCOMPILE) -c $(TOP)/ext/misc/json1.c  
    $(TCC) -shared -o json1.dylib json1.o  
make
很挑剔,所以一定要确保
$(LTCOMPILE)
$(TCC)
前面有制表符,而不是空格

然后运行
makejson1.dylib


参考:

下载源代码有效,但在我试图编译它时返回了一个错误。它是这样的:
/usr/lib/gcc/x86\u 64-pc-linux-gnu/6.2.1/../../../../../../../../../lib/crt1.o:在函数“\u start':(.text+0x20):未定义对“main”collect2的引用:错误:ls返回了1个退出状态
。然而,我看到其他人有我的问题,但他们解决了,发现它已经加载和启用。如果通过
pragma compile_options
发现并尝试它,那么json1也已经加载并为我启用了!抱歉重复评论。目前返回:
未找到架构x86_64的符号
,我已经尝试了很多方法,但都无法实现。macos:gcc-g-fPIC-dynamiclib-I.-I src ext/misc/json1.c-o json1在最新版本的OS X@JeffreyKnight中,上面的命令似乎不再有效
.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"}
json1.dylib: json1.lo  
    $(LTCOMPILE) -c $(TOP)/ext/misc/json1.c  
    $(TCC) -shared -o json1.dylib json1.o