Search Hen record usage of "SQLite" One way to save data of Android Apps
Using the database "SQLite" of Android?
This will be the sequel of the following articles.
Hen SQLiteOpenHelper use of "SQLite" one of the ways of data storage Android app
Edit, update and delete additional lines of how to use the "SQLite" One way to save data of Android Apps
So far, we have and a description of the database is open for update and delete records are added.
This time, we will describe the search statement select, the record referred to in the SQL.
Can not say unconditionally, I think on the deal with the database, there are lots of scenes to implement most of the code, and I do not select this.
The sample code shown from this is, " Hen SQLiteOpenHelper use of SQLite "One way to save data of Android apps" you shall have posted at the sample code in that it creates an instance already SQLiteDatabase type.
Variable names and sdb.
In addition, the table definition, in the "bookmarklist" the table name and column 2 of that column and row _id bookmark exists.
※ For _id column, the article "Another point to note when using SQLite to create the tables in the Android application please refer to the ".
After a search of records, search results, it will be returned as an instance called Cursor.
How to get Cursor this instance, since there are two big, I'll show you how that first.
Using the () SQLiteDatabase.query
The first one, shows you how to use () method first SQLiteDatabase.query.
final String [] columns = new String [] {"_id", "bookmark"};
String where = "bookmark like?";
String param = "% android%";
Cursor c =
sdb.query ("bookmarklist", columns, where, new String [] {param},
null, null, "_id desc", "10");
The first argument () method is a table name SQLiteDatabase.query.
Second argument specifies the column name to retrieve (column names, field names) an array of.
Record to specify the criteria for the third argument, the fourth argument to be obtained.
This time, we have to condition that, a string that contains the phrase, "android" to the string of bookmark.
The fifth argument specifies the clause group by.
The sixth argument specifies the clause Having.
The seventh argument specifies the clause order by.
The eighth argument, you specify the (maximum number of records to be retrieved) clause limit.
If you do not want to use, specify a null.
And to express this in the SQL statement processing, will be as follows.
select _id, bookmark
from bookmarklist
where bookmark like '% android%'
order by _id desc
limit 10
How to operate the Cursor objects are described below.
Using the () SQLiteDatabase.rawQuery
The following describes how to use the () method rawQuery So.
rawQuery () method is, literally, use the raw query.
String sqlstr = "select _id, bookmark" +
"From bookmarklist" +
"Where bookmark like '% android%'" +
"Order by _id desc" +
"Limit 10";
Cursor c = sdb.rawQuery (sqlstr);
() Method argument is a statement SQL SQLiteDatabase.rawQuery.
Personally, I think, but easy to use than the query () method of the above, what if.
query () method, the mean, it was good if you give the parameters of what to which arguments? I do not seem to be and should not examine and Ichiichi () method is clear rawQuery.
Besides, such as using the left outer join and inner join, when SQL was like to join multiple tables, you'll be better of () rawQuery this.
It may be love love, SQL of degree are used in Android app does not seem so difficult, I found the () rawQuery can execute raw SQL, and whether not it be nice readability.
Personally, I is the end.
Incidentally, the () is a statement only select rawQuery.
insert, update, delete, etc., use the () method execSQL.
For more information, please see below.
Edit, update and delete additional lines of how to use the "SQLite" One way to save data of Android Apps
So, let's look at the basic usage of the Cursor instance.
Using the Cursor instance
I think basically, and that usage will, to do something from beginning to end in a loop by turning the retrieved records.
Please see the sample code like the following example.
/ / Execute () rawQuery or () query
Optional - - Cursor c =;
if (c.moveToFirst ()) {
do {
long id = c.getLong (c.getColumnIndex ("_id"));
String bookmark = c.getString (c.getColumnIndex ("bookmark"));
System.out.println (id + ":" + bookmark);
} While (c.moveToNext ());
}
But that is just standard output all records obtained simply, let's look at from above.
First, in the first sentence if it has been run () Cursor. MoveToFirst.
This means that, move the cursor to the beginning for the results obtained.
True will be returned if successful.
Subsequent processing is that, to execute () After the success of this moveToFirst.
Incidentally, if the number of records retrieved was of 0, () return value will be false moveToFirst.
Next, it has become a judgment after the loop.
Implication is Cursor. MoveToNext () is called, move the cursor to the next record.
The return value () returns a true move the cursor if there is a record of this moveToNext next.
Returns false if there is no next record.
By the way, and that after the judgment is for the processing of the very first record.
Processing to extract the contents of the records, according to the type of each column, use, etc. () getInt), getString (), getLong (.
Argument is an index into the column, whether in the second column In short what? Is a number.
This, with the name of the column name, you can get in () getColumnIndex.
Roundabout is a specification · indescribably.
Did not take me as overload, even if you specify the column name in the argument, why? Just want to say.
Of course, if you know the index, but also good at writing without spaces without using the method to work correctly even if I change the table definition in the middle, they'd better be implemented as of this time I think whether it is not.
I think in such as a servlet or JSP, and you can understand without difficulty if you have experience in the system using the DB.
However, It is my understanding that the built-mobile, and until now I did not deal with the database or, what was generally understood.











