Is it possible to store Python class objects in SQLite?

后端 未结 9 2035
独厮守ぢ
独厮守ぢ 2020-12-22 17:42

I would like to store Python objects into a SQLite database. Is that possible?

If so what would be some links / examples for it?

9条回答
  •  [愿得一人]
    2020-12-22 18:10

    There is relatively simple way to store and compare objects, eaven to index those objects right way and to restrict (with ubique) columns containing objects. And all of that without using ORM engines. Objects mast be stored using pickle dump (so performance might be a issue) Here is example for storing python tuples, indexing restricting and comparing. This method can be easily applied to any other python class. All that is needed is explained in python sqlite3 documentation (somebody already posted the link). Anyway here it is all put together in the following example:

    import sqlite3
    import pickle
    
    def adapt_tuple(tuple):
        return pickle.dumps(tuple)    
    
    sqlite3.register_adapter(tuple, adapt_tuple)    #cannot use pickle.dumps directly because of inadequate argument signature 
    sqlite3.register_converter("tuple", pickle.loads)
    
    def collate_tuple(string1, string2):
        return cmp(pickle.loads(string1), pickle.loads(string2))
    
    # 1) Using declared types
    con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
    
    con.create_collation("cmptuple", collate_tuple)
    
    cur = con.cursor()
    cur.execute("create table test(p tuple unique collate cmptuple) ")
    cur.execute("create index tuple_collated_index on test(p collate cmptuple)")
    
    
    ######################### Test ########################
    
    cur.execute("select name, type  from sqlite_master") # where type = 'table'")
    print(cur.fetchall())
    
    p = (1,2,3)
    p1 = (1,2)
    
    cur.execute("insert into test(p) values (?)", (p,))
    cur.execute("insert into test(p) values (?)", (p1,))
    cur.execute("insert into test(p) values (?)", ((10, 1),))
    cur.execute("insert into test(p) values (?)", (tuple((9, 33)) ,))
    cur.execute("insert into test(p) values (?)", (((9, 5), 33) ,))
    
    try:
        cur.execute("insert into test(p) values (?)", (tuple((9, 33)) ,))
    except Exception as e:
        print e
    
    cur.execute("select p from test order by p")
    print "\nwith declared types and default collate on column:"
    for raw in cur:
        print raw
    
    cur.execute("select p from test order by p collate cmptuple")
    print "\nwith declared types collate:"
    for raw in cur:
        print raw
    
    con.create_function('pycmp', 2, cmp)
    
    print "\nselect grater than using cmp function:"
    cur.execute("select p from test where pycmp(p,?) >= 0", ((10, ),) )
    for raw in cur:
        print raw
    
    cur.execute("select p from test where pycmp(p,?) >= 0", ((3,)))
    for raw in cur:
        print raw 
    
    print "\nselect grater than using collate:"
    cur.execute("select p from test where p > ?", ((10,),) )
    for raw in cur:
        print raw  
    
    cur.execute("explain query plan select p from test where p > ?", ((3,)))
    for raw in cur:
        print raw
    
    cur.close()
    con.close()
    

提交回复
热议问题