Check If DB Is Empty

Question:
How do I check if a DB is empty? I need it for the project I am working on or an alternative that does the same thing.

Repl link:
https://replit.com/@SalladShooter/Salad-Forums#main.py

def loggedIn():
  clear()
  print("Welcome To The Salad Forums!\n")
  print("[1] Add Topic")
  print("[2] View Topics")
  choice = int(input("-> "))
  if choice == 1:
    clear()
    if db["topic"]["topicNumber"] != None:
      x = db["topic"]["topicNumber"]
    else:
      x = 0
    title = input("Topic Title: ")
    contents = input("-> ")
    x += 1
    db["topic"]["topicNumber"] = x
    db["topic"][title] = {"title" : title, "contents" : contents}
    db["topic"]["topicnumber"] = x
    loggedIn()
  elif choice == 2:
    clear()
    topic = input("Topic You Want To View: ")
    if topic not in db["topic"]:
      print(f"Error - The Topic '{topic}' Doesn't Exist")
    else:
      print("[ " + db["topic"]["topicNumber"] + " ] " + db["topic"][topic]["title"] + " : " + db["topic"][topic]["contents"])
1 Like
if db.keys():
    print("DB not empty")
# OR
if not db.keys():
    print("DB is empty")
3 Likes

try:

import json
import os
from replit import db

backup_file_path = 'backup.json'


def create_backup():
  backup_data = dict(db)

  with open(backup_file_path, 'w') as file:
    json.dump(backup_data, file, indent=2)


def load_backup():
  if os.path.exists(backup_file_path):
    with open(backup_file_path, 'r') as file:
      backup_data = json.load(file)
      db.update(backup_data)


def save_backup():
  backup_data = dict(db)

  with open(backup_file_path, 'w') as file:
    json.dump(backup_data, file)


def sync_backup():
  with open(backup_file_path, 'r') as file:
    backup_data = json.load(file)

  db.update(backup_data)

  create_backup()

just do

create_backup()

you can view the JSON file to check if it contains any keys.
this is more of a visual approach.
instead of what @python660 wrote.

1 Like

umm… is that a bit of an overkill?

2 Likes

@Idkwhttph I just want to use DB not JSON and both seems like a bit much.

4 Likes

9 posts were split to a new topic: Idkwhttph and hashing passwords

then just do what python660 did:

if db.keys():
    print("DB not empty")
#OR
if not db.keys():
    print("DB is empty")
1 Like

@Idkwhttph I haven’t tried python660’s code I’m working on the .md for your module. I’ll try it in a bit.

1 Like

@python660 How would I format the code with this stuff? Like this?

if db["topic"]["topicNumber"] not db.keys():
  x = db["topic"]["topicNumber"]
else:
  x = 0
1 Like

what does if item not iterable: mean

1 Like

@SalladShooter to test for “topicNumber”, you could use a ternary operator or a .get chain
e.g.

# Ternary
x = db["topic"]["topicNumber"] if db.keys() else 0

# .get chain
x = db.get("topic", dict()).get("topicNumber", 0)
2 Likes

2 posts were split to a new topic: SalladShooter: Website commissions

@python660 I now get an error with Line 27:

db["topic"][title] = {"title" : title, "contents" : contents}
1 Like

Whats the error? Keyerror?

1 Like

@python660

1 Like

Ah, you used db["topic"] which raises an error if “topic” doesnt exist. Check if topic is in db.keys() before executing line 27

1 Like

Which can be done like so:

if "topic" in db.keys():
1 Like

@Firepup650 db.keys() doesn’t work it’s just db

1 Like

@python660 I got the creating a topic working, now how do I fix the viewing?

elif choice == 2:
    clear()
    title = input("Topic You Want To View: ")
    if "topic" in db:
      print("[ " + db["topic"]["topicNumber"] + " ] " + db["topic"][title]["title"] + " : " + db["topic"][title]["contents"])
    else:
      print(f"Error - The Topic '{title}' Doesn't Exist")
1 Like

Huh. I think .keys() should work there, what’s the error?

1 Like