ubuntu PHP 启用 Sqlite

$ sudo apt-get install php5-cli php5-dev make
$ sudo apt-get install libsqlite3-0 libsqlite3-dev
$ sudo apt-get install php5-sqlite

<?php
###
### http://zetcode.com/databases/sqlitephptutorial/
###

#phpinfo();
echo sqlite_libversion();
echo "<br>";
echo phpversion();

######################################################################

dbhandle = sqlite_open('db/test.db', 0666,error);
if (!dbhandle) die (error);

$stm = "CREATE TABLE Friends(Id integer PRIMARY KEY," .
"Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN ('M', 'F')))";
ok = sqlite_exec(dbhandle, stm,error);

if (!$ok)
die("Cannot execute query. $error");

echo "Database Friends created successfully";

sqlite_close($dbhandle);

 

########################################################################

dbhandle = sqlite_open('db/test.db', 0666,error);

if (!dbhandle) die (error);

$stm1 = "INSERT INTO Friends VALUES(1,'Jane', 'F')";
$stm2 = "INSERT INTO Friends VALUES(2,'Thomas', 'M')";
$stm3 = "INSERT INTO Friends VALUES(3,'Franklin', 'M')";

ok1 = sqlite_exec(dbhandle, $stm1);
if (!$ok1) die("Cannot execute statement.");

ok2 = sqlite_exec(dbhandle, $stm2);
if (!$ok2) die("Cannot execute statement.");

ok3 = sqlite_exec(dbhandle, $stm3);
if (!$ok3) die("Cannot execute statement.");

echo "Data inserted successfully";

sqlite_close($dbhandle);

###################################################################
dbhandle = sqlite_open('db/test.db', 0666,error);

if (!dbhandle) die (error);

$query = "SELECT Name, Sex FROM Friends";
result = sqlite_query(dbhandle, $query);
if (!$result) die("Cannot execute query.");

row = sqlite_fetch_array(result, SQLITE_ASSOC);
print_r($row);
echo "<br>";

sqlite_rewind($result);
row = sqlite_fetch_array(result, SQLITE_NUM);
print_r($row);
echo "<br>";

sqlite_rewind($result);
row = sqlite_fetch_array(result, SQLITE_BOTH);
print_r($row);
echo "<br>";

sqlite_close($dbhandle);
?>