PHP code example of workbunny / storage
1. Go to this page and download the library: Download workbunny/storage library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
workbunny / storage example snippets
$client = new \WorkBunny\Storage\Driver([
# 内存数据库
'filename' => ':memory:',
# test.db文件数据库
// 'filename' => 'test.db',
'flags' => 'SQLITE3_OPEN_READWRITE|SQLITE3_OPEN_CREATE',
'encryptionKey' => ''
]);
# 注册执行结束回调事件
\WorkBunny\Storage\Driver::onAfterExec(function (\WorkBunny\Storage\Driver $driver){
# 打印sql及执行时长
var_dump($driver->last(true));
});
$client = new \WorkBunny\Storage\Driver(['filename' => ':memory:']);
# 预处理执行
$res = $client->execute('SELECT * FROM `account` WHERE `id` = 1;');
if($res instanceof SQLite3Result){
var_dump($res->fetchArray());
# 受影响行数
$client->driver()->changes();
# 最后插入的行号
$client->driver()->lastInsertRowID();
}
# 普通执行
$res = $client->query('SELECT * FROM `account` WHERE `id` = 1;');
if($res instanceof SQLite3Result){
var_dump($res->fetchArray());
# 受影响行数
$client->driver()->changes();
# 最后插入的行号
$client->driver()->lastInsertRowID();
}
# 仅执行
// 成功返回true 失败返回false
$res = $client->exec('SELECT * FROM `account` WHERE `id` = 1;');
$client = new \WorkBunny\Storage\Driver(['filename' => ':memory:']);
$client->create('account', [
'id' => [
'INT',
'PRIMARY KEY',
'NOT NULL',
],
'name' => [
'VARCHAR(25)',
'NOT NULL',
],
]);
$client = new \WorkBunny\Storage\Driver(['filename' => ':memory:']);
$client->create('account', [
'id' => [
'INT',
'PRIMARY KEY',
'NOT NULL',
],
'name' => [
'VARCHAR(25)',
'NOT NULL',
],
],[
'CREATE INDEX `account_name` ON `account` (`name`);'
]);
$client = new \WorkBunny\Storage\Driver(['filename' => ':memory:']);
$client->drop('account');
$client = new \WorkBunny\Storage\Driver(['filename' => ':memory:']);
# 一次插入单条
$client->insert('account', [
'id' => 1,
'name' => 'test'
]);
# 一次插入多条
$client->insert('account', [
[
'id' => 1,
'name' => 'test1'
],
[
'id' => 2,
'name' => 'test2'
]
]);
$client = new \WorkBunny\Storage\Driver(['filename' => ':memory:']);
# 开启
$client->begin();
# 回滚
$client->rollback();
# 提交
$client->commit();
# 事务执行
$client->action(function () {
$client->insert('account', [
'id' => 1,
'name' => 'test1'
]);
$client->insert('account', [
'id' => 2,
'name' => 'test2'
]);
$client->insert('account', [
'id' => 3,
'name' => 'test3'
]);
# 返回false或者异常抛出则回滚
});