php-sqlite

connect

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
try{
$db = new PDO('sqlite:/var/www/php56/db/pduService.db', null,null,
array(PDO::ATTR_PERSISTENT => true));

$sql = "
INSERT INTO t_demo
(name, age)
VALUES
(:name,:age);
";
$handler->debug($sql, 'db');
try {


$stmt = $db->prepare($sql);
$name = 'Tom';
$age = 32;
// Bind parameters to statement variables, 两种方法都可以
// $stmt->bindParam(1, $name);
// $stmt->bindParam(2, $age);
$stmt->bindValue(':name', 'Tom', SQLITE3_TEXT);
$stmt->bindValue(':age', 34, SQLITE3_INTEGER);

$bRet = $stmt->execute();
if ($bRet == false)
{
$handler->debug($db->errorInfo(),"db error");
}else{
$handler->debug("execute sql success","db success");
}
$handler->debug($stmt->rowCount(), 'db rowCount');
$handler->debug($db->lastInsertId(),"db lastInsertId");
} catch (PDOException $e) {
$handler->debug($e->getMessage(), 'db exception');
exit($e->getMessage());
}
$result = $db->query("SELECT * FROM t_demo") or die("query failed.");
var_dump($result->fetchAll());
}catch (PDOException $e)
{
echo $e->getMessage();
}