Write an Android platform all the problems (i)--problems encountered when querying the SQLite database

Source: Internet
Author: User

I am in the undergraduate graduation stage, graduation design to do an Android platform, for Java or small white I do have a bit of difficulty, but in the process is still a lot of harvest, now hope to put themselves in the process encountered in the various problems and friends to share, the gods do not like to spray, because the graduation design to do six months, So the next series is almost half a year to write, I try to persist.

Let's get started!!

I. Problems encountered while querying SQLite database

For all Android developers, SQLite must not be unfamiliar, today I first contact on her abused for 40 minutes, really ashamed. Other operations on the database I will not say more, we must be very clear, I would like to focus on the query database encountered problems.

Querying the database half of what we use is cursor this class, about this class I personally feel she a bit like C language pointers, with bad on the problem, was cursorindexoutofboundsexception abuse of people in the heart silently answer to. In general, the problem is that you did not point the cursor object to the first row of the table. The solution is simple, too.

Cursor C = db.query (table,columns,selection,selectionargs,null,null,null);    if (C.movetofirst ())    {            //Read data code    }    else{    System.out.println ("no Record");    }

Note: The above code is just a sample code, did not write completely, I would like to say that I began to see the code on the Internet to come directly to the use of the later found not to know not to write completely, after my code in addition to the complete method code and program code, the other only as an example, if you want to use (I am a bit You have to add it yourself.

To get back to the point, today I have a problem with the use of Movetofirst () and move (int offset), directly on the code description:

Public String querydata_query (sqlitedatabase db,string table,string[]columns,string selection,string[] SelectionArgs              {    Cursor c = db.query (Table,columns,selection,selectionargs,null,null,null); Queries and obtains    the cursor for (int i=0;i<c.getcount (); i++)    {    if (C.movetofirst ())//Determines whether the cursor is empty    {    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;    }

Move this method is very disturbing to me, everyone notice that her parameter is offset, that is, the offset value, indicating the position of the next cursor = the current cursor position +offset, rather than on the web some place to say the table of a row, and to use this method, you must ensure that the starting address is unchanged, That is to ensure that the C.movetofirst () at each time the offset value to be executed once, which is a bit like a single-chip base + address of the address method.

Write an Android platform all the problems (i)--problems encountered when querying the SQLite database

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.