站內搜尋

Thursday, April 17, 2014

[SQLite][PHP] 如何在 PHP 中使用 SQLite (快速入門)

一、如何建立資料庫:
SQLite會自動判斷,資料庫是否存在,如果不存在才會建立。

try{
        $db_conn = new PDO('sqlite:mydb.sqlite');
}catch(PDOException $e){
        echo 'Error';
}
$stmt = "CREATE TABLE mytable(id, user)";
$db_conn->exec($stmt);


二、如何寫入資料:
使用方式和 MySQL 差不多。
$stmt = "INSERT INTO mytable(id, user) VALUES('1', 'AAA')";
$db_conn->exec($stmt);


三、如何使用資料:
使用方式和 MySQL 差不多。

$sth = $db_conn->prepare("SELECT id, user FROM mytable");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
        echo $row['id'].':'.$row['user'].'<br />';
}

No comments:

Post a Comment