As I mentioned at the end of the previous section, I’m going to show you a couple of additional methods bundled with the SQLite library that can be valuable. They work well for those situations where you want to use an alternative way to run queries, and for determining the number of rows affected after performing a DML statement. The first method that I’ll teach you is “queryExec().” It consists of a simple replacement of the “query()” method that you learned before. Here’s how to use it: // example using the 'queryExec()' method // create new database using the OOP approximation $db=new SQLiteDatabase("db.sqlite"); $query="INSERT INTO users (id,name,email) VALUES(NULL,'John if(!$db->queryExec($query)){ trigger_error('Error performing } If you take some time and examine the above short example, you’ll understand why I said the “queryExec()” method can be used as an alternative to the previously reviewed “query().” In this case, the example speaks for itself, therefore I suggest you pay attention to the following code sample. It is much more useful, since it illustrates a basic application of the brand new “changes()” method. The script listed below shows precisely how you can use this method to calculate the number of affected row after running a DML statement: // create new database using the OOP approximation $db=new SQLiteDatabase("db.sqlite"); // insert new row into 'USERS' database table $db->query("INSERT INTO users (id,name,email) VALUES echo 'Number of rows modified after the insertion '.$db->changes(); /* // displays the following Number of rows modified after the insertion 1 As you can see in the above example, the “changes()” method can be really helpful if you want to know how many rows were affected after inserting, updating or deleting records of a particular database. Of course, this method is closely similar to the PHP “mysql_affected_rows()” function, therefore you shouldn’t have too many problems understanding how it works. All right, at this stage I believe that you’ve been provided with a neat set of SQLite methods which can be used for tackling different tasks. However, we’ve not come to the end of the tutorial yet, since there are a few more methods that remain uncovered. Speaking of that, in the following section, I’ll teach you how to use iterators to traverse different result sets, and how to define custom functions with SQLite as well. Therefore, jump straight into the next lines and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|