public function testCrud() { // テーブル作成 Qb::connect("sqlite:{$this->db_name}.db"); Qb::db()->exec('DROP TABLE IF EXISTS test'); Qb::db()->exec('CREATE TABLE test(id INTEGER PRIMARY KEY, no INTEGER, name VARCHAR(255))'); // INSERT $id = Qb('test')->save(['no' => 100, 'name' => '太郎']); $this->assertEquals(1, $id); $r = Qb('test')->where('no', 100)->oneArray(); $this->assertEquals(['id' => 1, 'no' => 100, 'name' => '太郎'], $r); $r = Qb('test')->where('id', 1)->oneArray(); $this->assertEquals(['id' => 1, 'no' => 100, 'name' => '太郎'], $r); $r = Qb('test')->where(1)->oneArray(); $this->assertEquals(['id' => 1, 'no' => 100, 'name' => '太郎'], $r); $r = Qb('test')->oneArray('no', 100); $this->assertEquals(['id' => 1, 'no' => 100, 'name' => '太郎'], $r); $r = Qb('test')->oneArray(1); $this->assertEquals(['id' => 1, 'no' => 100, 'name' => '太郎'], $r); // UPDATE $id = Qb('test')->save(['no' => 200, 'name' => '二郎']); $this->assertEquals(2, $id); Qb('test')->where($id)->save(['no' => 300, 'name' => '三郎']); $r = Qb('test')->oneArray($id); $this->assertEquals(['id' => $id, 'no' => 300, 'name' => '三郎'], $r); Qb('test')->where(3)->save(['no' => 400, 'name' => '四郎']); $r = Qb('test')->oneArray(3); $this->assertEquals(['id' => 3, 'no' => 400, 'name' => '四郎'], $r); Qb('test')->where(4)->update(['no' => 500, 'name' => '五郎']); $r = Qb('test')->oneArray(4); $this->assertEmpty($r); // DELETE Qb('test')->where(1)->delete(); $r = Qb('test')->oneArray(1); $this->assertEmpty($r); Qb('test')->delete(2); $r = Qb('test')->oneArray(2); $this->assertEmpty($r); Qb('test')->delete(); $r = Qb('test')->oneArray(3); $this->assertEmpty($r); }
<?php require_once '../qb.php'; Qb::connect('sqlite:sample.db'); Qb::db()->exec('CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY, title TEXT, completed INTEGER)'); if (!empty($_GET['action'])) { switch ($_GET['action']) { case 'list': echo Qb('todo')->toJson(); exit; case 'post': Qb('todo')->save(['title' => $_GET['title'], 'completed' => 0]); exit; case 'delete': Qb('todo')->where('id', $_GET['id'])->delete(); exit; case 'change': Qb('todo')->where('id', $_GET['id'])->save('completed', $_GET['completed']); exit; } } ?> <!DOCTYPE html> <html lang="ja" ng-app="app"> <head> <meta charset="UTF-8"> <title>ToDo</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script> <script> angular.module('app', []).controller('MainController', function($scope, $http) { function api(action, params) {
<?php require_once '../qb.php'; Qb::connect('sqlite:sample.db'); Qb::db()->exec('CREATE TABLE IF NOT EXISTS contact(id INTEGER PRIMARY KEY, name TEXT, mail TEXT)'); if (!empty($_POST['name'])) { Qb('contact')->save(['name' => $_POST['name'], 'mail' => $_POST['mail']]); header('Location: ' . $_SERVER['REQUEST_URI']); exit; } $contacts = Qb('contact')->toObject(); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>Contact</title> </head> <body> <ul> <?php foreach ($contacts as $contact) { echo "<li>{$contact->name} <a href='mailto:{$contact->mail}'>{$contact->mail}</a></li>"; } ?> </ul> <form method="post"> name:<input name="name"> mail:<input name="mail"> <input type="submit" value="post"> </form>
/** * データベースに接続する。 * * @param string $dsn 接続文字列 * @param string $user ユーザー * @param string $pass パスワード */ public static function connect($dsn, $user = '', $pass = '') { self::$db = new PDO($dsn, $user, $pass); self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); }
/** * クエリ発行 内部使用 * * @param string $sql SQL文 * * @return PDOStatement PDOStatement */ protected function _query($sql) { $binds = array_merge($this->set_binds, $this->condition_binds); $st = self::$db->prepare($sql); $st->execute($binds); self::$last_sql = $sql; return $st; }