用PHP操作SQLite数据库
a、 如何连接sqlite数据库?
if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {
#select * from sqlite_master;这里是查询语句代码
echo "数据库连接成功!";
} else {
die($sqliteerror);
}
b、 如何列出数据库中所有的表?
if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {
$result =sqlite_array_query($db, 'select * from sqlite_master;');
foreach ($result as $entry) {
echo 'talbe name='.$entry['name']."
n";
echo 'sql='.$entry['sql']."
n";
echo "
--------------------------------------------------------------------------------
";
}
sqlite_close($db);
} else {
die($sqliteerror);
}}
c、 对sqlite数据库的查询,以及结果集的显示(select数据使用sqlite_array_query函数)
if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {
#链接数据库,$db为sqlite路径,第二个语句为查询语句,第三个语句为返回结果集的格式。
$result = sqlite_array_query($db, 'select name, email from user ', SQLITE_ASSOC);
echo "user表查询结果:
n";
echo " n name email
n";
//遍历结果集
foreach ($result as $entry) {
echo ' '.$entry['name']." " $entry['email']."
n";
}
echo ' ';
sqlite_close($db);
} else {
die($sqliteerror);
}
d、数据库对象记录的增加、删除、修改(增删改数据使用sqlite_query函数执行)
sqlite_query($db, "INSERT INTO user VALUES('user".$i."'" ",'user".$i."@ hichina. com')");
sqlite_query($db, "delete from user where user=’user99’");
sqlite_query($db, 'UPDATE user SETemail="lilz@ hichina .com" where name="user1"');