All problems encountered when writing an Android platform (I) -- problems encountered when querying the sqlite database, androidsqlite

Source: Internet
Author: User

All problems encountered when writing an Android platform (I) -- problems encountered when querying the sqlite database, androidsqlite

I am at the graduation stage of my undergraduate course. I have to build an Android platform for my graduation project. I am a little difficult with java or even a little bit of white, but I have gained a lot in the process of doing this, now I want to share all the problems I encountered in this process with my friends. I don't like it because I have to spend half a year on my graduation project, so it will take nearly half a year for this series to come. I will stick to it as much as possible.

Let's get started !!

I. problems encountered when querying the sqlite Database

It must be said that sqlite is no stranger to all Android Developers. Today, I was abused by her for 40 minutes in my first contact. I will not talk much about other database operations. Everyone must be very clear. I will focus on the problems encountered during database query.

We use the cursor class to query half of the database. I personally think this class is a bit like a pointer in C. If it is not used well, it will lead to problems, those who have been abused by CursorIndexOutOfBoundsException will answer this question silently. In general, this problem is because you have not pointed the cursor object to the first row of the table. The solution is also simple.

Cursor c = db. query (table, columns, selection, selectionArgs, null); if (c. moveToFirst () {// read data code} else {System. out. println ("no record ");}

Note: The above code is just a sample code. It is not completely written. At the beginning, when I saw the code on the internet, I took it directly. Later I found that I could not use it to know that I didn't write it completely, in the future, besides the complete method code and program code, my code will only be used as an example. If you want to use it (I am a little cheeky, haha), add it yourself.

Continue back to the topic. Today, I encountered a problem about how to use moveToFirst () and move (int offset). The Code is described directly:

Public String queryData_query (SQLiteDatabase db, String table, String [] columns, String selection, String [] selectionArgs) {Cursor c = db. query (table, columns, selection, selectionArgs, null); // query and obtain the cursor for (int I = 0; I <c. getCount (); I ++) {if (c. moveToFirst () // determines whether the cursor is null {c. move (I); // move to the specified record username = c. getString (c. getColumnIndex ("username"); System. out. println ("query -------> username =" + username);} else {System. out. println ("no record"); break;} c. close (); return username ;}

This method of moving is very difficult for me. We note that her parameter is offset, that is, the offset value, indicating that the next cursor position is the current cursor position + offset, instead of saying something on the internet that represents a row of a table, you must ensure that the starting address remains unchanged, that is, c. moveToFirst () is executed once every time the offset value is changed. This is a bit like addressing mode of address + address change in single chip microcomputer.

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.