-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_start.php
105 lines (92 loc) · 3.21 KB
/
quick_start.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use KSQLite\KSQLite;
use KSQLite\KSQLiteQueryContext;
// Before using KSQLite, it's important to load the FFI definitions once.
// For KPHP, this call can be placed in the beginning of your script.
if (KPHP_COMPILER_VERSION) {
KSQLite::loadFFI();
}
// Creating a new KSQLite object can't fail.
// But we're not connected to the database yet.
//
// By default, KSQLite handles have $auto_close set to true.
// It means that $db->close will be called automatically in the end of this
// script (via shutdown function).
// It's still safe to call $db->close() manually.
//
// Note: you don't have to close $db handle until you open() the actual connection.
$db = new KSQLite();
// Calling open() attempts to open the database file.
if (!$db->open('testdb')) {
// In the real code, you'll do some proper error-handling.
// For the simplicity purposes, we'll just stop the script execution on errors.
// But still, we're handling errors explicitely.
handle_error(__LINE__, 'open', $db->getLastError());
}
$query = '
CREATE TABLE IF NOT EXISTS languages(
lang_id INTEGER PRIMARY KEY,
lang_name TEXT NOT NULL,
first_appeared INTEGER NOT NULL,
num_elephants REAL NOT NULL
);
';
// exec() runs a query and discards the results.
if (!$db->exec($query)) {
handle_error(__LINE__, 'exec', $db->getLastError());
}
// fetchColumn() executes a query and returns on of its columns.
// It's like SQLite3::querySingle with $entireRow=false.
// The second return value $ok is false on errors.
[$count, $ok] = $db->fetchColumn('SELECT COUNT (*) FROM languages');
if (!$ok) {
handle_error(__LINE__, 'fetchColumn', $db->getLastError());
}
echo "count=$count\n";
if ($count === 0) {
$rows = [
['PHP', 1995, 1.0],
['KPHP', 2014, 2.0],
['C', 1972, 0.0],
['C++', 1983, 0.0],
['JavaScript', 1995, 0.0],
['Go', 2009, 0.0],
];
// For simplicity, we're using the string interpolation with VALUES
// here, but you should never do so with untrusted inputs.
// Use param binding API for that.
foreach ($rows as $row) {
[$lang_name, $first_appeared, $num_elephants] = $row;
$query = "
INSERT INTO languages(lang_name, first_appeared, num_elephants)
VALUES('$lang_name', $first_appeared, $num_elephants)
";
if (!$db->exec($query)) {
handle_error(__LINE__, 'exec/insert', $db->getLastError());
}
}
}
// fetchRowAssoc() is like fetchColumn(), but it fetches all columns
// instead of just one.
//
// As a side note, we're using a tuple instead of false to express
// the $row type as mixed[], which is not compatible with false.
// For consistency, all methods try to report error in this way.
[$row, $ok] = $db->fetchRowAssoc('SELECT * FROM languages LIMIT 1');
if (!$ok) {
handle_error(__LINE__, 'fetchRow', $db->getLastError());
}
var_dump(['test row' => $row]);
// fetch reads all results into an array of arrays.
[$rows, $ok] = $db->fetch('SELECT * FROM languages');
if (!$ok) {
handle_error(__LINE__, 'fetchRow', $db->getLastError());
}
foreach ($rows as $row) {
var_dump("$row[lang_id] => $row[lang_name]");
}
echo "OK\n";
function handle_error(int $line, string $op, string $error) {
die("line $line: error: $op: $error\n");
}