File tree Expand file tree Collapse file tree 9 files changed +193
-0
lines changed
Expand file tree Collapse file tree 9 files changed +193
-0
lines changed Original file line number Diff line number Diff line change 1+ # TinyDB: A Lightweight JSON Database for Small Projects
2+
3+ This repository contains the downloadable code for the Real Python tutorial: [ TinyDB: A Lightweight JSON
4+ Database for Small Projects] ( https://realpython.com/tinydb-python/ )
5+
6+ ## Contents
7+
8+ - ` create_db.py ` — Code from the ** CREATE** section of the tutorial
9+ - ` read.py ` — Code from the ** READ** section of the tutorial
10+ - ` update_db.py ` — Code from the ** UPDATE** section of the tutorial
11+ - ` update_db_v2.py ` — Additional update code from the ** UPDATE** section
12+ - ` update_db_v3.py ` — Additional update code from the ** UPDATE** section
13+ - ` delete.py ` — Code from the ** DELETE** section of the tutorial
14+ - ` countries_file.csv ` — CSV data used to create TinyDB documents
15+ - ` ten_countries.json ` — Sample JSON file containing data for ten countries
Original file line number Diff line number Diff line change 1+ location , population , continent
2+ Argentina , 45929925 , South America
3+ Switzerland , 8990428 , Europe
4+ Mozambique , 36147236 , Africa
Original file line number Diff line number Diff line change 1+ from csv import DictReader
2+
3+ from tinydb import TinyDB
4+
5+ with TinyDB ("countries.json" , indent = 4 ) as countries_db :
6+ countries_table = countries_db .table (name = "countries" )
7+
8+ countries_table .insert ({"location" : "Vatican City" , "population" : 501 })
9+
10+ countries_table .insert_multiple (
11+ [
12+ {"location" : "India" , "population" : 1_417_492_000 },
13+ {"location" : "China" , "population" : 1_408_280_000 },
14+ ]
15+ )
16+
17+ with open ("countries_file.csv" , "r" ) as csv_source :
18+ reader = DictReader (csv_source )
19+
20+ for row in reader :
21+ row ["population" ] = int (row ["population" ])
22+ countries_table .insert (row )
Original file line number Diff line number Diff line change 1+ # REPL code
2+
3+ from tinydb import TinyDB , where
4+
5+ countries_db = TinyDB ("ten_countries.json" )
6+ countries_table = countries_db .table (name = "countries" )
7+ len (countries_table )
8+
9+ countries_table .remove (doc_ids = [3 , 5 , 7 ])
10+
11+ len (countries_table )
12+
13+ countries_table .remove (where ("population" ) < 300_000_000 )
14+ len (countries_table )
15+
16+ countries_table .truncate ()
17+ len (countries_table )
18+
19+ countries_db .tables ()
20+
21+ countries_db .drop_table (name = "countries" )
22+ countries_db .tables ()
Original file line number Diff line number Diff line change 1+ # REPL Code
2+
3+ from pprint import pprint
4+
5+ from tinydb import Query , TinyDB
6+
7+ countries_db = TinyDB ("ten_countries.json" )
8+ countries_table = countries_db .table (name = "countries" )
9+ query = Query ()
10+ query_def = (query .population > 220_000_000 ) & (query .population < 250_000_000 )
11+ pprint (countries_table .search (query_def ))
12+ pprint (countries_table .search (query_def ))
13+ pprint (countries_table .search (query ["% of world" ] >= 17 ))
14+ pprint (countries_table .get (doc_ids = [9 , 10 ]))
15+ countries_db .close ()
Original file line number Diff line number Diff line change 1+ {
2+ "countries" : {
3+ "1" : {
4+ "location" : " India" ,
5+ "population" : 1417492000 ,
6+ "% of world" : 17.3 ,
7+ "date" : " 1 Jul 2025" ,
8+ "source" : " Official projection"
9+ },
10+ "2" : {
11+ "location" : " China" ,
12+ "population" : 1408280000 ,
13+ "% of world" : 17.2 ,
14+ "date" : " 31 Dec 2024" ,
15+ "source" : " Official estimate"
16+ },
17+ "3" : {
18+ "location" : " United States" ,
19+ "population" : 340110988 ,
20+ "% of world" : 4.1 ,
21+ "date" : " 1 Jul 2024" ,
22+ "source" : " Official estimate"
23+ },
24+ "4" : {
25+ "location" : " Indonesia" ,
26+ "population" : 284438782 ,
27+ "% of world" : 3.5 ,
28+ "date" : " 30 Jun 2025" ,
29+ "source" : " National annual projection"
30+ },
31+ "5" : {
32+ "location" : " Pakistan" ,
33+ "population" : 241499431 ,
34+ "% of world" : 2.9 ,
35+ "date" : " 1 Mar 2023" ,
36+ "source" : " 2023 Census result"
37+ },
38+ "6" : {
39+ "location" : " Nigeria" ,
40+ "population" : 223800000 ,
41+ "% of world" : 2.7 ,
42+ "date" : " 1 Jul 2023" ,
43+ "source" : " Official projection"
44+ },
45+ "7" : {
46+ "location" : " Brazil" ,
47+ "population" : 213421037 ,
48+ "% of world" : 2.6 ,
49+ "date" : " 1 Jul 2025" ,
50+ "source" : " Unknown"
51+ },
52+ "8" : {
53+ "location" : " Bangladesh" ,
54+ "population" : 169828911 ,
55+ "% of world" : 2.1 ,
56+ "date" : " 14 Jun 2022" ,
57+ "source" : " 2022 Census result"
58+ },
59+ "9" : {
60+ "location" : " Russia" ,
61+ "population" : 146028325 ,
62+ "% of world" : 1.8 ,
63+ "date" : " 1 Jan 2025" ,
64+ "source" : " ???"
65+ },
66+ "10" : {
67+ "location" : " Mexico" ,
68+ "population" : 0 ,
69+ "% of world" : 1.6 ,
70+ "date" : " 30 Jun 2025" ,
71+ "source" : " ???"
72+ }
73+ }
74+ }
Original file line number Diff line number Diff line change 1+ from tinydb import TinyDB , where
2+
3+ with TinyDB ("ten_countries.json" ) as countries_db :
4+ countries_table = countries_db .table (name = "countries" )
5+
6+ countries_table .update (
7+ {"population" : 130_575_786 }, where ("location" ) == "Mexico"
8+ )
9+
10+ countries_table .update (
11+ {"source" : "National quarterly update" },
12+ where ("location" ) == "Mexico" ,
13+ )
14+
15+ # REPL code.
16+ # from pprint import pprint
17+ # from tinydb import TinyDB, where
18+ # with TinyDB("ten_countries.json") as countries_db:
19+ # countries_table = countries_db.table(name="countries")
20+ # pprint(countries_table.search(where("location") == "Mexico"))
Original file line number Diff line number Diff line change 1+ from tinydb import TinyDB , where
2+
3+ with TinyDB ("ten_countries.json" ) as countries_db :
4+ countries_table = countries_db .table (name = "countries" )
5+ countries_table .update_multiple (
6+ [
7+ (
8+ {"population" : 130_575_786 },
9+ where ("location" ) == "Mexico" ,
10+ ),
11+ (
12+ {"source" : "National quarterly update" },
13+ where ("location" ) == "Mexico" ,
14+ ),
15+ ]
16+ )
Original file line number Diff line number Diff line change 1+ from tinydb import TinyDB
2+
3+ with TinyDB ("ten_countries.json" ) as countries_db :
4+ countries_table = countries_db .table (name = "countries" )
5+ countries_table .update ({"source" : "Official estimate" }, doc_ids = [7 , 9 ])
You can’t perform that action at this time.
0 commit comments