存取SQLite資料庫的說明如下:
SQLiteDatabase類別提供2種查詢功能的方法
1.rawQuery():僅接受單純的SQL查詢語法
rawQuery(String sql, String[] selectionArgs) 2.query():將SQL查詢語法依照query()的參數來切割 query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
1.設計Ch8_2_BrowseData.java
- package com.example.ch8_2_browsedata;
- import java.util.ArrayList;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class Ch8_2_BrowseData extends Activity {
- EditText et1,et2,et3,et4;
- Button btn1,btn2;
- TextView tv2;
- DBHP dbHlp;
- ArrayList sites;
- int index=0;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- connectDB();
- findViews();
- showSites(index);
- }
- private void connectDB() {
- if(dbHlp == null)
- dbHlp = new DBHP(this);
- dbHlp.fillDB();
- sites=dbHlp.getAllSites();
- }
- void findViews(){
- et1=(EditText)findViewById(R.id.editText1);
- et2=(EditText)findViewById(R.id.editText2);
- et3=(EditText)findViewById(R.id.editText3);
- et4=(EditText)findViewById(R.id.editText4);
- btn1=(Button)findViewById(R.id.button1);
- btn2=(Button)findViewById(R.id.button2);
- tv2=(TextView)findViewById(R.id.textView2);
- btn1.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- index=index+1;
- if(index>=sites.size())
- index=0;
- showSites(index);
- }} );
- btn2.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- index=index-1;
- if(index<0)
- index=sites.size()-1;
- showSites(index);
- }} );
- }
- void showSites(int index){
- if(sites.size()>0){
- tv2.setText((index+1)+"/"+sites.size()+getString(R.string.count));
- et1.setText(sites.get(index).getId());
- et2.setText(sites.get(index).getName());
- et3.setText(sites.get(index).getPhoneNo());
- et4.setText(sites.get(index).getAddress());
- }else{
- tv2.setText("0/0"+getString(R.string.count));
- et1.setText("");
- et2.setText("");
- et3.setText("");
- et4.setText("");
- }
- }
- }
復制代碼
2.設計DBHP.java
- package com.example.ch8_2_browsedata;
- import java.util.ArrayList;
- 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;
- public class DBHP extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "sites";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_NAME = "sitesInfo";
- private static final String TABLE_CREATE =
- "CREATE TABLE " + TABLE_NAME + " (id TEXT NOT NULL,name TEXT NOT NULL,phoneNo TEXT, address TEXT, PRIMARY KEY (id)); ";
- public DBHP(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(TABLE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
- public void fillDB() {
- SQLiteDatabase db = getWritableDatabase();
- ContentValues[] values = new ContentValues[3];
- for(int i=0; i
- values[i] = new ContentValues();
- values[0].put("id", "yangmingshan");
- values[0].put("name", "陽明山國家公園管理處");
- values[0].put("phoneNo", "02-28613601");
- values[0].put("address", "台北市北投區竹子湖路1之20號");
- values[1].put("id", "yushan");
- values[1].put("name", "玉山國家公園管理處");
- values[1].put("phoneNo", "049-2773121");
- values[1].put("address", "南投縣水里鄉中山路一段300號");
- values[2].put("id", "taroko");
- values[2].put("name", "太魯閣國家公園管理處");
- values[2].put("phoneNo", "03-8621100");
- values[2].put("address", "花蓮縣秀林鄉258號");
- for(ContentValues row : values){
- db.insert(TABLE_NAME, null, row);
- }
- db.close();
- }
- public ArrayList getAllSites()
- {
- SQLiteDatabase db = getReadableDatabase();
- String columns[]={"id","name","phoneNo","address"};
- Cursor cursor =db.query(TABLE_NAME, columns, null, null, null, null, null);
- ArrayList sites = new ArrayList();
- while(cursor.moveToNext()){
- String id=cursor.getString(0);
- String name=cursor.getString(1);
- String phoneNo=cursor.getString(2);
- String address=cursor.getString(3);
- Site site=new Site(id,name,phoneNo,address);
- sites.add(site);
- }
- cursor.close();
- db.close();
- return sites;
- }
- }
復制代碼
3.設計Site.java
- package com.example.ch8_2_browsedata;
- public class Site {
- String id,name,phoneNo,address;
- Site(String id,String name,String phoneNo,String address)
- {
- this.id=id;
- this.name=name;
- this.phoneNo=phoneNo;
- this.address=address;
- }
- String getId(){
- return id;
- }
- String getName(){
- return name;
- }
- String getPhoneNo(){
- return phoneNo;
- }
- String getAddress(){
- return address;
- }
- }
復制代碼
|