Some people are just confused to use this. Even with the documentation.
Here is how to use it:
pip install replit
if you haven’t done this.After that, do:
from replit import db
or
import replit
It is best to do
from replit import db
Now great! You have done the first step to harnessing the power of replit db, now, you can store db keys like so:
from replit import db
db['key'] = 'bool/int/str'
Replit stores keys like dictionaries ( key value pairs )
it is very simiar to a dictionary like so:
#replit db
from replit import db
db['Key'] = 'Val'
#Dict
dictionary = {}
dictionary['key'] = 'val'
print(dictionary)
So it gets really easy when you have learnt dicts in python.
Now, lets dive deeper into what replit db can do!
Replit db can
- Extract values from the database
from replit import db
db['key'] = 'Value'
print(db['key'])
- Delete keys
del db['key']
-
Work with other databases. Eg. A JSON database. We will dive deeper into this later.
-
db.prefix(str)
And way more!
Now, you can create a ton of keys, and to do so, you can do something like this:
from replit import db
for i in range(1000):
db['key'+str(i)] = 'value'
You would create 1000 keys with their own values
You can print all the keys like so:
from replit import db
print(db.keys())
Remember! db.keys() Is a function! If you do not put it as db.keys()
and put it as db.keys
you won’t be able to get all the keys from the db
Same for the values, do:
from replit import db
print(db.values())
Remember! db.values() Is a function! If you do not put it as db.values()
and put it as db.values
you won’t be able to get all the values from the db. And one common mistake is that you do put it as a function but you do this:
from replit import db
print(db.value())
remember, its db.values() not db.value()!
next, we will be moving onto some more complex things to do with replit db.
Now, lets say you want to unpack data from your database, you can do it like so:
from replit import db
def unpack_data():
global var1,var2,var3 # all the vars you need
var1 = db['var1']
var2 = db['var2']
#REMEMBER TO CALL THE FUNCTION!
unpack_data()
You can also use .prefix
from replit import db
db['hello world'] = 'hi'
db['hello world 12131231'] = 'none'
matches = db.prefix('hello world')
print('matches')
Now, there are some downsides to the replit database, one of them is that you can’t see the entire db unless you print it out, this is where the other databases come in place, here is an example with the JSON database to visualise data better in your database!
from replit import db
db['hi'] = 'Hello'
db['Damn'] = 'Why damn!'
backup_file_path = "backup.json"
def create_backup(data_base):
backup_data = dict(data_base)
with open(backup_file_path, "w") as file:
json.dump(backup_data, file, indent=2)
create_backup(db)
and you should see that a backup.json file has been created, from there, you can better visualise your data instead of just seeing how many keys you have stored in the database.
This is just a brief explanation of how to use this database, if you want to find out more, you can check out the replit db docs. If you want to use a data visualiser/backup db, you can use the package idkwhttph
just do
pip install idkwhttph==0.3.2
next, you can do
from replit import db
from idkwhttph import create_backup,sync_backup,load_backup,save_backup
db['hello'] = 'hi!'
create_backup()
You can also go into the backup.json to change the data, and then you can do
from idkwhttph import sync_backup
sync_backup()
and the databases will be synced!