We have studied the SQLite data Traversal method and summarized it below:
By: Chen yunwen
1 use iterator of SQLite: foreach ()
Use the iterator of SQLite to traverse the results after obtaining the handle of the queried dataset through sqlite_query ().
The routine is as follows:
$ Db = new sqlite_db ("testdb ");
$ Db-> query ("
Insert into myfood (name, price) values ('cookies', 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 );
The iterator here is very similar to the iteration sub-iterator in STL. You can use it to access the queried dataset one by one. Here, the foreach () method can access the current record of the handle object, and automatically loops to the next record until the result obtained by the SELECT command is fully accessed. Here, the Select * command is used, and the actual result is that all the elements in the mydrings table are traversed
In addition to foreach (), the following iterator functions can be used:
Sqlite_has_more ()Returns true if there are still records in the result set to determine whether the traversal is complete.
Sqlite_current ()Obtains the row data of the currently pointed record.
Sqlite_next ()Point the iterator pointer to the next record
Related usage:
$ Db = sqlite_open ("testdb ");
$ Result = sqlite_query ("select * From myfood ");
While (sqlite_has_more ($ result )){
$ ROW = sqlite_current ($ result );
Echo $ row ['id'], "", $ row ['name'], "", $ row ['price'], "/N ";
Sqlite_next ($ result );
2 Use sqlite_fetch_array ()
Sqlite_fetch_array () provided by SQLite. A good function is that after the data in the current row is returned for processing, it automatically points to the following row until the set traversal returned by query () ends. For example:
$ Db = sqlite_open ("testdb ");
Sqlite_query ($ db,
"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 = sqlite_query ($ db, "select * From myfood ");
While ($ ROW = sqlite_fetch_array ($ result )){
Echo $ row ['id'], "", $ row ['name'], "", $ row ['price'], "/N ";
}
Sqlite_close ($ dB );
3. Use the sqlite_array_query () command directly.
SQLite also provides the sqlite_array_query () command, which is equivalent to combining query () and sqlite_fetch_array ().
It is easier to implement the SQL query statement and the subsequent result traversal function once:
$ 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 );
4. How to Use the callback function
Exec (sqlstr, callback, callpara)
Sqlstr is set to string:
"Select * From myfood where status = 0 limit 100"
Then, all the status values in the set selected by the update operation are 1 (which indicates that it has been read)
This method can control the number of sets selected each time. When the database is very large, the performance is better than the use of select *