by:陈运文
1 使用sqlite的iterator:foreach()
通过sqlite_query()获得查询数据集的句柄后,使用sqlITE的iterator来遍历结果
例程如下:
$db = new sqlite_db("TestDb");
$db->query("
INSERT INTO MyFood (name,price) values ('Cookie',255.95);
INSERT INTO MyFood (name,price) values ('Pretz',155.95);
INSERT INTO MyFood (name,price) values ('Beer',120.45);
");
$result = $db->query("SELECT * FROM MyFood ");
foreach($result as $row ) {
echo $row['id']," ",$row['name'],$row['price'],"/n";
}
unset($db);
这里的iterator和STL中的迭代子iterator非常相似,可以通过它完成对查询数据集的逐一访问;这里foreach()方法可以访问句柄对象当前记录,并自动循环到下一条记录,直到SELECT命令取出的结果完全被访问到;这里使用了SELECT * 命令,其实际结果就是将MyDrings table里面的元素全部遍历到了
除了foreach() 以外,还有以下iterator function可以使用:
相关的用法如:
$db = sqlite_open("TestDb");
$result = sqlite_query("SELECT * FROM MyFood ");
while (sqlite_has_more($result)) {
$row = sqlite_current($result);
echo $row['id'],"/n";
sqlite_next($result);
2 使用sqlite_fetch_array()
sqlite中提供的sqlite_fetch_array() ,非常好的功能是在返回当前row的数据进行处理后,会自动指向下面一行,直到query()返回的set遍历结束,例如:
$db = sqlite_open("TestDb");
sqlite_query($db,
"INSERT INTO MyFood (name,price) values (Beer,120.45)
");
$result = sqlite_query($db,"SELECT * FROM MyFood ");
while ($row = sqlite_fetch_array($result)) {
echo $row['id'],"/n";
}
sqlite_close($db);
3 直接使用sqlite_array_query() 命令
sqlite中还提供了sqlite_array_query()命令,这条命令相当于将query() 和 sqlite_fetch_array() 组合在一起
$db = sqlite_open("TestDb");
$result = sqlite_array_query($db,"SELECT * FROM MyFood ");
foreach ($result as $row) {
echo $row['name'] . " = " . $row['price'] . "/n";
}
sqlite_close($db);
exec(sqlstr,callback,callpara)
sqlstr设置为string:
"SELECT * FROM MyFood where status =0 limit 100"
然后update所select出的set中所有status为1(表示已被读出)
原文链接:https://www.f2er.com/sqlite/202933.html