DEV Community

Free Python Code
Free Python Code

Posted on

How to use SmileyDB3 to make a database

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
Enter fullscreen mode Exit fullscreen mode

let's create a database and create a new table

Create database

from SmileyDB3 import SmileyDB3

db = SmileyDB3('mydb.db')
Enter fullscreen mode Exit fullscreen mode

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
})
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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'}]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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'}]}]
Enter fullscreen mode Exit fullscreen mode

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}
)
Enter fullscreen mode Exit fullscreen mode

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'}]}]
Enter fullscreen mode Exit fullscreen mode

Delete record

from SmileyDB3 import SmileyDB3

db = SmileyDB3('mydb.db')

tasks = db.table('tasks')

tasks.Delete(uuid='ffccb7ff-02c6-440e-92e2-8eba0f271fe9')
Enter fullscreen mode Exit fullscreen mode


`

Top comments (0)