Python - SQLite JSON1 load extension

后端 未结 2 458
醉梦人生
醉梦人生 2021-02-02 01:40

I want to use the json1 extension for SQLite within Python. According to the official documentation, it should be a loadable extension. I got the json1.c file from the source an

2条回答
  •  梦毁少年i
    2021-02-02 01:52

    you can run sqlite with python 3. here is what worked for me on my mac:

    first compile the loadable extension:

    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
    

    then use it in a script:

    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()
    

    or in a shell:

    .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"}
    

    i wish this was easier. it seems like loading extensions (rather than building them into sqlite on creation) makes a lot more sense. my latest issue is that i cant seem to compile the json1 extension on CentOS 6.

    i wrote a guide here: https://github.com/SMAPPNYU/smapphowto/blob/master/howto_get_going_with_sqlite_json1.md

    EDIT: i eventually gave up on json1 for my purposes. i now just use pysmap dump_to_csv to column based csv by extracting the fields i want, and then dump_to_sqlite_db create a normal sqlite db from that csv. see pysmap smapp_collection

提交回复
热议问题