使用SQLiteDatabase物件查詢資料表中的資料,

需要用到SQL中的SELECT敘述。

 

  • SELECT敘述基本語法如下:
SELECT * FROM TableName
SELECT name, phone, email FROM TableName

 

  • 使用Cursor物件取得查詢結果:

Cursor可稱為資料指標,要查詢某一筆紀錄必須將Cursor指標指到它,

才能讀取其內容,以下方法可移動Cursor指標

moveToFirst(), moveToLast(), moveToNext(), moveToPrevious()

rawQuery("SELECT * FROM "+tb_name, null); //傳回test資料表的所有紀錄

 

  • 使用Cursor物件的getXXX()方法讀取資料
Cursor c=db.rawQuery("SELECT * FROM "+tb_name, null);
c.getString(0);
c.getString(1);
c.getString(2);

 

此範例使用Cursor物件讀取查詢結果

MainActivity.java

import tw.com.flag.ch15_hellosqlite.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    static final String db_name="testDB";    // 資料庫名稱
    static final String tb_name="test";        // 資料表名稱
    SQLiteDatabase db;    //資料庫
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 開啟或建立資料庫
        db = openOrCreateDatabase(db_name,  Context.MODE_PRIVATE, null);
        //db.delete(tb_name, null,null);
        String createTable="CREATE TABLE IF NOT EXISTS " + 
                tb_name +
                "(name VARCHAR(32), " +
                "phone VARCHAR(16), " +
                "email VARCHAR(64))";
        db.execSQL(createTable);    // 建立資料表

        Cursor c=db.rawQuery("SELECT * FROM "+tb_name, null);    // 查詢tb_name資料表中的所有資料
        if (c.getCount()==0){    // 若無資料, 則立即新增 2筆資料 
            addData("Flag Publishing Co.","02-23963257","service@flag.com.tw");
            addData("PCDIY Magazine","02-23214335","service@pcdiy.com.tw");
            c=db.rawQuery("SELECT * FROM "+tb_name, null);    // 重新查詢
        }
        
        if (c.getCount()>0){    // 若有資料
            String str="總共有 "+c.getCount()+"筆資料\n";
            str+="-----\n";
            
            c.moveToFirst();    // 移到第 1 筆資料                
            do{        // 逐筆讀出資料
                str+="name:"+c.getString(0)+"\n";
                str+="phone:"+c.getString(1)+"\n";
                str+="email:"+c.getString(2)+"\n";        
                str+="-----\n";
            } while(c.moveToNext());    // 有一下筆就繼續迴圈

            TextView txv=(TextView)findViewById(R.id.txv);
            txv.setText(str);
        }
        
        db.close();        // 關閉資料庫        
    }

    private void addData(String name, String phone, String email) {
        ContentValues cv=new ContentValues(3);
        cv.put("name", name);
        cv.put("phone", phone);
        cv.put("email", email);

        db.insert(tb_name, null, cv);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

 

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hellosqlite.MainActivity" >

    <TextView
        android:id="@+id/txv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textSize="22sp" />

</RelativeLayout>

 

arrow
arrow
    文章標籤
    android SQLite
    全站熱搜

    Will 發表在 痞客邦 留言(0) 人氣()