Hi 🙂🖐
Welcome back, In this post, I will share with you my new Python library
SmileyDB3
SmileyDB3 is a library built on SQLite3 to make working with databases easier.
install SmileyDB3
pip install SmileyDB3
let's create a database and create a new table
Create database
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
Create a new table
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
tasks = db.table('tasks')
tasks.Insert(data = {
'name': 'This is test name',
'description': 'This is tasj description',
'coins': 50
})
Insert list of data using InsertMany function
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
tasks = db.table('tasks')
data_list = [
{
'name': 'This is test name 1',
'description': 'This is tasj description 1',
'coins': 20
},
{
'name': 'This is test name 2',
'description': 'This is tasj description 2',
'coins': 100
}
]
tasks.InsertMany(data_list=data_list)
Get data or records by columns names like coins, name, description
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
tasks = db.table('tasks')
result = tasks.GetBy(coins = 20)
print(result)
Result
[{'_index': 1, 'name': 'This is test name 1', 'description': 'This is tasj description 1', 'coins': 20, 'uuid': 'ffccb7ff-02c6-440e-92e2-8eba0f271fe9', 'created_at': '2024-08-14 20:34:26.274377'}]
Filter data
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
tasks = db.table('tasks')
result = tasks.Filter(filter_keys={'coins': {'larger_than': 10}})
print(result)
Result
[{'key': 'coins', 'records': [{'_index': 1, 'name': 'This is test name 1', 'description': 'This is tasj description 1', 'coins': 20, 'uuid': 'ffccb7ff-02c6-440e-92e2-8eba0f271fe9', 'created_at': '2024-08-14 20:34:26.274377'}, {'_index': 2, 'name': 'This is test name 2', 'description': 'This is tasj description 2', 'coins': 100, 'uuid': 'f691204a-fa50-4fce-9b16-46146bfe73a8', 'created_at': '2024-08-14 20:34:26.515023'}]}]
you can use larger_than, less_than, not, equal, between
Update records
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
tasks = db.table('tasks')
tasks.Update(
uuid = 'ffccb7ff-02c6-440e-92e2-8eba0f271fe9',
data = {'coins': 1000}
)
Result
[{'key': 'coins', 'records': [{'_index': 1, 'name': 'This is test name 1', 'description': 'This is tasj description 1', 'coins': 20, 'uuid': 'ffccb7ff-02c6-440e-92e2-8eba0f271fe9', 'created_at': '2024-08-14 20:34:26.274377'}, {'_index': 2, 'name': 'This is test name 2', 'description': 'This is tasj description 2', 'coins': 100, 'uuid': 'f691204a-fa50-4fce-9b16-46146bfe73a8', 'created_at': '2024-08-14 20:34:26.515023'}]}]
Delete record
from SmileyDB3 import SmileyDB3
db = SmileyDB3('mydb.db')
tasks = db.table('tasks')
tasks.Delete(uuid='ffccb7ff-02c6-440e-92e2-8eba0f271fe9')
`