最近用上了Sqlite,感觉挺轻量的 适合保存少量的数据 下面是数据库的操作文档:
连接数据库:(PDO方式)
<?php
try {
$pdo = new PDO("sqlite:database.db");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "连接到数据库时发生错误: " . $e->getMessage();
}
增:
<?php
$pdo->exec("INSERT INTO users (name) VALUES ('John')");
?>
删
<?php
$pdo->exec("DELETE FROM users WHERE id = 1");
?>
查:
<?php
$ret = $pdo->query("SELECT * FROM users");
//$row = $ret->fetch(PDO::FETCH_ASSOC);
//$row['user'];
while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . "<br>";
}
?>
改:
<?php
$pdo->exec("UPDATE users SET name = 'Jane' WHERE id = 1");
?>
评论 (1)