Android Notes (41) data storage in Android--sqlite (iii) SELECT

Source: Internet
Author: User

SQLite implements queries through query, which defines query conditions through a series of parameters.

Description of each parameter:

Query () method parameters corresponding SQL section Describe
Table From table_name Table name
Colums Select Column1,column2 Array of column names
Selection where column = value Conditional clauses, equivalent to where
Selectionargs - Parameter array for conditional statements
GroupBy Group BY column Group
Having Having column = value Grouping conditions
By ORDER BY Column,column Sort class
Limit Limitations of paged Queries
Cursor The return value, equivalent to the result set resultset

There are also a number of ways to target cursors (cursor)

Method name Method description
GetCount () Total number of records
IsFirst () Determine if the first record
Islast () Determine if the last record
Movetofirst () Move to the first record
Movetolast () Move to the last record
Move (int offset) Move to the specified record
MoveToNext () Move to Next record
Movetoprevious () Move to previous record
Getcolumnindex (String columnName) Gets the int type value of the specified column index

Here's an example to illustrate the query in SQLite:

Query with no parameters

Mainactivity.java

 PackageCn.lixyz.sqlite;Importandroid.app.Activity;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText name, age; PrivateButton Insertbutton, Selectbutton; Privatesqlitedatabase database; PrivateMysqliteopenhelper Msop;  PublicString Inputsex; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Findview (); Msop=NewMysqliteopenhelper ( This, "User.db",NULL, 1); Database=msop.getreadabledatabase (); }    Private voidFindview () {name=(EditText) Findviewbyid (r.id.name); Age=(EditText) Findviewbyid (r.id.age); Insertbutton=(Button) Findviewbyid (R.id.insertbutton); Selectbutton=(Button) Findviewbyid (R.id.selectbutton); }     Public voidClickbutton (view view) {Switch(View.getid ()) { CaseR.id.selectbutton:selectdata ();  Break;  CaseR.id.insertbutton:insertdata ();  Break; }    }    Private voidInsertData () {String inputage=Age.gettext (). toString (); String InputName=Name.gettext (). toString (); Contentvalues CV=Newcontentvalues (); Cv.put ("Name", InputName); Cv.put ("Age", Inputage); Database.insert ("User",NULL, CV); Toast.maketext (mainactivity. This, "Insert succeeded", Toast.length_short). Show (); Age.settext (""); Name.settext (""); }    Private voidSelectdata () {Cursor C= Database.query ("User",NULL,NULL,NULL,NULL,NULL,NULL); if(C.movetofirst ()) { Do {                intid = c.getint (c.getcolumnindex ("id"))); String name= C.getstring (C.getcolumnindex ("name")); String Age= C.getstring (C.getcolumnindex ("Age")); LOG.D ("Tttt", "id=" + ID + ", name =" + Name + ", age =" +Age ); }  while(C.movetonext ());    } c.close (); }    classMysqliteopenhelperextendsSqliteopenhelper {Private Static FinalString create_user = "CREATE table USER (ID integer primary key autoincrement,name text,age text)"; PrivateContext Mcontext;  PublicMysqliteopenhelper (context context, String name, Cursorfactory factory,intversion) {            Super(context, name, Factory, version); Mcontext=context; } @Override Public voidonCreate (Sqlitedatabase db) {db.execsql (create_user); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {            //TODO auto-generated Method Stub        }    }}

Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <EditTextAndroid:id= "@+id/name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Enter Name" />    <EditTextAndroid:id= "@+id/age"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Input Age" />    <ButtonAndroid:id= "@+id/insertbutton"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Clickbutton"Android:text= "click Insert" />    <ButtonAndroid:id= "@+id/selectbutton"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Clickbutton"Android:text= "click Query" /></LinearLayout>

Insert a few data first, then click the Query button:

  

Android Notes (41) data storage in Android--sqlite (iii) SELECT

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.