-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexample.php
95 lines (73 loc) · 1.84 KB
/
example.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
<?php
// database file name
$database = "example.db";
// database password
$password = "123456";
// returns new instance of PDO object
function getPDO($database) {
try {
$pdo = new PDO("sqlcipher:" . $database);
} catch (PDOException $e) {
die($e->getMessage() . PHP_EOL);
}
return $pdo;
}
//
// Create new example database with one table
// (it can be done through sqlcipher command line client)
//
$sql = "PRAGMA key = '$password';
PRAGMA encoding = \"UTF-8\";
PRAGMA auto_vacuum = 2;
PRAGMA incremental_vacuum(10);
CREATE TABLE `test` (
`id` INTEGER NOT NULL PRIMARY KEY,
`value` TEXT NOT NULL
);";
$pdo = getPDO($database);
if ($pdo->exec($sql) === false) {
$error = $pdo->errorInfo();
die($error[0] . ": " . $error[2] . PHP_EOL);
}
//
// Use example database
//
// set encryption key before any sql command
$sql = "PRAGMA key = '$password'";
$pdo = getPDO($database);
if ($pdo->exec($sql) === false) {
$error = $pdo->errorInfo();
die($error[0] . ": " . $error[2] . PHP_EOL);
}
// insert rows
$sql = "INSERT INTO `test` VALUES (1, 'value1');
INSERT INTO `test` VALUES (2, 'value2');
INSERT INTO `test` VALUES (3, 'value3');";
if ($pdo->exec($sql) === false) {
$error = $pdo->errorInfo();
die($error[0] . ": " . $error[2] . PHP_EOL);
}
// select rows
$result = $pdo->query("SELECT * FROM `test`");
if ($result === false) {
$error = $pdo->errorInfo();
die($error[0] . ": " . $error[2] . PHP_EOL);
}
foreach ($result as $row) {
print_r($row);
}
// alter table
$sql = "ALTER TABLE `test` RENAME TO `test2`";
if ($pdo->exec($sql) === false) {
$error = $pdo->errorInfo();
die($error[0] . ": " . $error[2] . PHP_EOL);
}
// select rows
$result = $pdo->query("SELECT * FROM `test2`");
if ($result === false) {
$error = $pdo->errorInfo();
die($error[0] . ": " . $error[2] . PHP_EOL);
}
foreach ($result as $row) {
print_r($row);
}