HadiDB (hadidb) 是一個用Python編寫的輕量級數據庫,具備高度水平的可擴展性。
安裝HadiDB
使用pip安裝:
立即學習“Python免費學習筆記(深入)”;
pip install hadidb
用戶管理
創建用戶:createuser() 方法創建一個新用戶。authentication() 方法驗證用戶身份。
from hadidb.operation import user user_obj = user("admin", "admin") user_obj.createuser() # 創建用戶 user_obj.authentication() # 驗證用戶
返回結果示例:
{'status': 200, 'message': 'database user created'}
數據庫、集合和模式創建
以下代碼片段展示了如何設置用戶憑據、數據庫集合架構,以及如何插入數據。
from hadidb.operation import operation username = "admin" password = "admin" database = "mefiz.com" Collection = "authuser" schema = { "username": "unique", "password": "hash", "cnic": "unique", "picture": "image", "bio": "text" } db = operation(username, password, database, collection) db.create_database(schema)
數據操作
- 插入數據: db.insert(data) 方法插入數據。
data = { "username": "hadidb", "password": "12345", "cnic": "123232442", "picture": "user/my/hadidb.jpg", "bio": "hadidb is the best ;)" } result = db.insert(data) print(result)
返回結果示例:
{ 'status': 200, 'message': 'data insert successfully', 'data': { 'username': 'hadidb', 'password': '12345', 'cnic': '123232442', 'picture': 'user/my/hadidb.jpg', 'bio': 'hadidb is the best ;)', 'id': 1 } }
- 更新數據: db.update(1, update_data) 方法更新數據。
update_data = { "username": "hadidb_update", "password": "123455", "cnic": "1232324423", "picture": "user/my/hadidb1.jpg", "bio": "hadidb is the best ;) update bio" } result = db.update(1, update_data) print(result)
-
按ID獲取數據: db.getbyid(1) 方法按ID獲取數據。
-
獲取所有數據: db.getall() 方法獲取所有數據。
-
按鍵獲取數據: db.getbykey() 和 db.getbykeys() 方法按鍵獲取數據。
-
計數: db.count() 方法統計數據數量。db.getbykeycount() 方法統計指定鍵值對匹配的數據數量。
-
刪除數據: db.delete(1) 方法刪除數據。
數據庫和集合管理
-
獲取所有數據庫: configuration().get_database() 方法獲取所有數據庫。
-
獲取所有集合: configuration(database).get_collection() 方法獲取指定數據庫的所有集合。
-
獲取模式: configuration(database, collection).get_schema() 方法獲取指定集合的模式。
-
刪除集合: databasedeletionservice().deletecollection() 方法刪除集合。
-
刪除數據庫: databasedeletionservice().deleteDatabase() 方法刪除數據庫。
項目鏈接
- gitHub: https://www.php.cn/link/e8630344970962450dbedfcd4cc6d718
- 網站: https://www.php.cn/link/2c6037040bf5058a44be4c0397611909
- 開發者: 莫明·伊克巴爾
這個版本對原文進行了重新組織和潤色,使其更清晰易讀,并對代碼部分進行了格式化,使其更易于理解。 所有圖片鏈接都保留了。