I have been trying to create multiple tables in a table using TinyDB. Here is a website to help you understand what TinyDb is (TinyDB PDF). The PDF file did not show how to insert multiple tables into one, one multiple data into one table.
我一直在尝试使用TinyDB在一个表中创建多个表。这里有一个网站可以帮助您了解TinyDb是什么(TinyDb PDF)。PDF文件没有说明如何将多个表插入到一个表中,将多个数据插入到一个表中。
I intended the json file to look like this:
我希望json文件是这样的:
"MASTER TABLE":
{
{"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}
{"TABLE 2": {"1": {"Name": "John", "Age": 12}},
}
However, the issue is that I'm not sure how to insert Table1 and Table2 into the Master file table. SO it gave me the error that table1 is not an element. I know it isn't an element but I don't have any idea of how to fix it, to put the two tables under the Master file table. I'd appreciate any help.
但是,问题是我不确定如何将表1和表2插入到主文件表中。它给了我一个错误表1不是一个元素。我知道它不是一个元素,但是我不知道如何修复它,将这两个表放在主文件表下面。我感谢任何帮助。
Here's my codes:
这是我的代码:
from tinydb import TinyDB, Query
from tinydb import TinyDB, where
import json
with open("/home/pi/Desktop/jsontest/test.json", 'w+'):
table1 = TinyDB('/home/pi/Desktop/jsontest/test.json')
table1 = table1.table('TABLE 1')
table1.insert_multiple([{'Name' : 'Alice' , 'Age' : 19}])
table2 = TinyDB('/home/pi/Desktop/jsontest/test.json')
table2 = table2.table('TABLE 2')
table2.insert_multiple([{'Name' : 'john' , 'Age' : 12}])
overall = TinyDB('/home/pi/Desktop/jsontest/test.json')
overall = overall.table('MASTER TABLE')
overall.insert([table1])
1 个解决方案
#1
1
It doesn't make sense to insert tables into another table ?
将表插入另一个表没有意义?
from tinydb import TinyDB
db = TinyDB('db.json')
table1 = db.table('TABLE 1')
table1.insert({'Name' : 'Alice' , 'Age' : 19})
table2 = db.table('TABLE 2')
table2.insert({'Name' : 'john' , 'Age' : 12})
Gives db.json containing:
给数据库。json:包含
{
"_default": {},
"TABLE 1": {"1": {"Name": "Alice", "Age": 19}},
"TABLE 2": {"1": {"Name": "john", "Age": 12}}
}
I think your JSON is incorrect, you cannot the syntax (key, value) here:
我认为您的JSON是不正确的,您不能在这里使用语法(key, value):
"MASTER TABLE":
{
{"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}
{"TABLE 2": {"1": {"Name": "John", "Age": 12}},
}
You can do that:
你可以这样做:
from tinydb import TinyDB
db = TinyDB('db.json', default_table='MASTER TABLE')
master_table = db.table('MASTER TABLE')
master_table.insert({"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}})
master_table.insert({"TABLE 2": {"1": {"Name": "John", "Age": 12}}})
You obtain db.json containing:
你获得db。json:包含
{
"MASTER TABLE": {
"1": {"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}},
"2": {"TABLE 2": {"1": {"Name": "John", "Age": 12}}}
}
}
But it is strange.
但这是奇怪的。
#1
1
It doesn't make sense to insert tables into another table ?
将表插入另一个表没有意义?
from tinydb import TinyDB
db = TinyDB('db.json')
table1 = db.table('TABLE 1')
table1.insert({'Name' : 'Alice' , 'Age' : 19})
table2 = db.table('TABLE 2')
table2.insert({'Name' : 'john' , 'Age' : 12})
Gives db.json containing:
给数据库。json:包含
{
"_default": {},
"TABLE 1": {"1": {"Name": "Alice", "Age": 19}},
"TABLE 2": {"1": {"Name": "john", "Age": 12}}
}
I think your JSON is incorrect, you cannot the syntax (key, value) here:
我认为您的JSON是不正确的,您不能在这里使用语法(key, value):
"MASTER TABLE":
{
{"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}
{"TABLE 2": {"1": {"Name": "John", "Age": 12}},
}
You can do that:
你可以这样做:
from tinydb import TinyDB
db = TinyDB('db.json', default_table='MASTER TABLE')
master_table = db.table('MASTER TABLE')
master_table.insert({"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}})
master_table.insert({"TABLE 2": {"1": {"Name": "John", "Age": 12}}})
You obtain db.json containing:
你获得db。json:包含
{
"MASTER TABLE": {
"1": {"TABLE 1": {"1": {"Name": "Alice", "Age": 19}}},
"2": {"TABLE 2": {"1": {"Name": "John", "Age": 12}}}
}
}
But it is strange.
但这是奇怪的。