Methods for Traversing data in SQLite

Source: Internet
Author: User

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 *

     

     

     

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.