Print "\nwith declared types and default collate on column:"Ĭur.execute("select p from test order by p collate cmptuple") Return cmp(pickle.loads(string1), pickle.loads(string2))Ĭon = nnect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)Ĭon.create_collation("cmptuple", collate_tuple)Ĭur.execute("create table test(p tuple unique collate cmptuple) ")Ĭur.execute("create index tuple_collated_index on test(p collate cmptuple)")Ĭur.execute("select name, type from sqlite_master") # where type = 'table'")Ĭur.execute("insert into test(p) values (?)", (p,))Ĭur.execute("insert into test(p) values (?)", (p1,))Ĭur.execute("insert into test(p) values (?)", ((10, 1),))Ĭur.execute("insert into test(p) values (?)", (tuple((9, 33)) ,))Ĭur.execute("insert into test(p) values (?)", (((9, 5), 33) ,))Ĭur.execute("select p from test order by p") Sqlite3.register_converter("tuple", pickle.loads) Sqlite3.register_adapter(tuple, adapt_tuple) #cannot use pickle.dumps directly because of inadequate argument signature Anyway here it is all put together in the following example: import sqlite3 All that is needed is explained in python sqlite3 documentation (somebody already posted the link). Here is example with tuples, that can be easily applied for any other python class. It is possible to store object data as pickle dump, jason etc but it is also possible to index, them, restrict them and run select queries that use those indices.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |