Android筆記(四十一) Android中的資料存放區——SQLite(三)select

來源:互聯網
上載者:User

標籤:

         SQLite 通過query實現查詢,它通過一系列參數來定義查詢條件。

         各參數說明:

query()方法參數 對應sql部分 描述
table from table_name 表名稱
colums select column1,column2 列名稱數組
selection where column = value 條件子句,相當於where
selectionArgs - 條件陳述式的參數數組
groupBy group by column 分組
having having column = value 分組條件
orderBy order by column,column 排序類
limit   分頁查詢的限制
Cursor   傳回值,相當於結果集ResultSet

 

 

         針對遊標(Cursor)也提供了不少方法

方法名稱 方法描述
getCount() 總記錄條數
isFirst() 判斷是否第一條記錄
isLast() 判斷是否最後一條記錄
moveToFirst() 移動到第一條記錄
moveToLast() 移動到最後一條記錄
move(int offset) 移動到指定的記錄
moveToNext() 移動到下一條記錄
moveToPrevious() 移動到上一條記錄
getColumnIndex(String columnName) 獲得指定列索引的int類型值

 

      下面我們通過例子來示範一下SQLite中的查詢:

   不帶參數查詢

MainActivity.java

package cn.lixyz.sqlite;import android.app.Activity;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private EditText name, age;    private Button insertButton, selectButton;    private SQLiteDatabase database;    private MySQLiteOpenHelper msop;    public String inputSex;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findView();        msop = new MySQLiteOpenHelper(this, "user.db", null, 1);        database = msop.getReadableDatabase();    }    private void findView() {        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 void clickButton(View view) {        switch (view.getId()) {        case R.id.selectButton:            selectData();            break;        case R.id.insertButton:            insertData();            break;        }    }    private void insertData() {        String inputAge = age.getText().toString();        String inputName = name.getText().toString();        ContentValues cv = new ContentValues();        cv.put("name", inputName);        cv.put("age", inputAge);        database.insert("user", null, cv);        Toast.makeText(MainActivity.this, "插入成功", Toast.LENGTH_SHORT).show();        age.setText("");        name.setText("");    }    private void selectData() {        Cursor c = database.query("user", null, null, null, null, null, null);        if (c.moveToFirst()) {            do {                int id = 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 + ",年齡=" + age);            } while (c.moveToNext());        }        c.close();    }    class MySQLiteOpenHelper extends SQLiteOpenHelper {        private static final String CREATE_USER = "create table user(id integer primary key autoincrement,name text,age text)";        private Context mContext;        public MySQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {            super(context, name, factory, version);            mContext = context;        }        @Override        public void onCreate(SQLiteDatabase db) {            db.execSQL(CREATE_USER);        }        @Override        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {            // TODO Auto-generated method stub        }    }}

 

activity_main.xml

<LinearLayout xmlns: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" >    <EditText        android:id="@+id/name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="輸入姓名" />    <EditText        android:id="@+id/age"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="輸入年齡" />    <Button        android:id="@+id/insertButton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="clickButton"        android:text="點擊插入" />    <Button        android:id="@+id/selectButton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="clickButton"        android:text="點擊查詢" /></LinearLayout>

  先插入幾條資料,然後點擊查詢按鈕:

  

 

Android筆記(四十一) Android中的資料存放區——SQLite(三)select

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.