简体   繁体   English

带有多个文件的 TinyDB 中的 Query()

[英]Query() in TinyDB with multiple files

I use Python + TinyDB, I have 2 json files, and I create 2 TinyDB objects:我使用 Python + TinyDB,我有 2 个 json 文件,我创建了 2 个 TinyDB 对象:

db = TinyDB(“db.json”)
forum = TinyDB(“forum.json”)

But how do I use Query() ?但是我该如何使用Query()呢? How can I code that Query() will only check db.json or vice versa?我如何编码Query()将只检查db.json反之亦然?

A "Query" in tiny db is a special Python object that override operators, so that when used they can be used by tinyDB engine in the "search" method to compare values in all records it can reach. tiny db 中的“查询”是一个特殊的 Python object 覆盖运算符,因此当使用它们时,tinyDB 引擎可以在“搜索”方法中使用它们来比较它可以到达的所有记录中的值。

It is made so that is not attached to any specific tinyDB object ("database"): it just binds any operands and operations to be performed on a subsequent search.这样做是为了不附加到任何特定的 tinyDB object(“数据库”):它只是绑定要在后续搜索中执行的任何操作数和操作。

It is the search that must be the .search method of a specific database or tinyDB object.搜索必须是特定数据库或tinyDB object的.search方法。

So, if you have two databases where you want to perform the same query, just do that, calling the .search method in one, and then on the other.所以,如果你有两个数据库,你想在其中执行相同的查询,只需这样做,在一个数据库中调用.search方法,然后在另一个数据库中调用。

But if you want to somehow have a search that depends on the two datasets, like a relational search: tinyDB does not do that.但是如果你想以某种方式进行依赖于两个数据集的搜索,比如关系搜索:tinyDB 不会那样做。 it is meant to be simple, and do not correlate data.它旨在简单,并且不关联数据。

If you need that, make a quick script to dump your tinyDB contents into SQLite databases, and then use plain SQL (or an ORM, lightweight or not), to do the query.如果你需要,制作一个快速脚本将你的 tinyDB 内容转储到 SQLite 数据库,然后使用普通 SQL(或 ORM,轻量级或非轻量级)进行查询。

Dumping a tinyDB base to a sqlite can be as simple as:将 tinyDB 库转储到 sqlite 可以很简单:

from tinydb import TinyDB
import sqlite3



def get_schema_from_db(db):
    schema = {}
    for record in db.all():
        for key, value in record.items():
            schema.setdefault(key, set()).add(type(value))
    return {key:('REAL' if float in v else 'INTEGER' if int in v else 'TEXT' if str in v else "BLOB") for key, v in schema.items()}


def create_sql_table(sqldb, name, schema):
    createsql = f"CREATE TABLE IF NOT EXISTS {name} (id INTEGER PRIMARY KEY, {{}})".format(", ".join(f"{key} {value}" for key, value in schema.items() ) )
    sqldb.execute(createsql)

def transfer(tydb, sqldb, schema, tablename):
    insertsql = f"INSERT INTO {tablename} ({', '.join(schema.keys())})  VALUES ({', '.join('?' for _ in range(len(schema)))})"
    sqldb.executemany(insertsql, [tuple(rec.values()) for rec in tydb.all()])

# And to use these 3 functions:

tydb = TinyDB("db.json")
sqldb = sqlite3.connect("sql.db")

schema = get_schema_from_db(tydb)
create_sql_table(sqldb, "mytable", schema)
transfer(tydb, sqldb, schema, "mytable")

Sorry if the code above looks too complicated: it is basically string manipulation to arrive at the final SQL forms - it is "complicated" but not "complex" - rather, quite straightforward.对不起,如果上面的代码看起来太复杂了:它基本上是字符串操作来达到最终的 SQL forms - 它“复杂”但不“复杂” - 相反,非常简单。

After that you have your tinyDB data in SQL form and can post queries joining different tables and what not.之后,您将拥有 SQL 形式的 tinyDB 数据,并且可以发布查询加入不同的表等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM