georgechem/sqlite-db

允许快速创建/使用 sqlite 数据库作为存储。

v1.1.0 2021-10-26 13:11 UTC

This package is auto-updated.

Last update: 2024-09-06 18:17:06 UTC


README

简单的 Sqlite 存储

存储应该仅用于简单的键值数据。应避免将对象存储作为不良做法,尽管它完全支持。为了存储对象,最好是创建一个单独的类并扩展抽象 Db 类。

示例

class User extends Db {
    self::$pdo - object is available 
    
    ... add your methods like: 
    public function insertUser() ... etc.
    
    Methods that must be implemented (required by abstarct class Db)
    
    public function create(){}
    public function destroy(){}
    public function empty(){}
    
}
mixed $value - it can be any type (under the hood $value is serialized and
unserialized so type is preserved);

string $key - as it states must be a string

使用 composer 安装

composer require georgechem/sqlite-db

像平时一样要求自动加载器

require __DIR__ . '/vendor/autoload.php';

用法

$storage = new Storage(); 

// save value associated with certain key in database (keys are unique)
$storage->save(string 'key_name', mixed $value);

// if you want to overwrite certain key set appropriate flag
$storage->save(string 'key_name', mixed $value, true);

// To read value associated wit certain key
$storage->read(string 'test');

// returns array of all available key in storage OR empty array if none
$storage->getAllKeys();

// empty storage (delete only entries but keep table)
$storage->empty();

// destroy storage
$storage->destroy();