Android開發—資料庫應用—訪問資料表(SQLite OpenHelper) —添加檢索操作(Retrieve)

來源:互聯網
上載者:User

/* (程式頭部注釋開始)
* 程式的著作權和版本聲明部分
* Copyright (c) 2011, 煙台大學電腦學院學生
* All rights reserved.
* 檔案名稱:資料庫應用—添加檢索操作(Retrieve)
* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月 14 日
* 版 本 號: V1.0
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程式輸出:
* 程式頭部的注釋結束
*/
 
 
以前用過的“db.rawQuery”語句可以用來直接做SQL查詢(程式如下),
[java]
public Cursor getall() {   
       return db.rawQuery("SELECT * FROM notes", null);   
   
    }   
並返回查詢結果的指標,如果想實現檢索功能,可以再程式中使用更具彈性的“query”方法,來替換直接執行“SQL”語句的“rawQuery”方法。
 
修改後的“DummyDbAdapter.java”檔案如下:
[java] 
package com.demo.android.dummynote; 
 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
 
public class NotesDbAdapter { 
    private static final String DATABASE_NAME = "notes.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_TABLE = "notes"; 
    private static final String DATABASE_CREATE = "creat table notes(" 
            + "_id INTEGER PRIMARY KEY," + "note TEXT," + "created INTEGER," 
            + "modified INTEGER" + ");"; 
 
    private static class DatabaseHelper extends SQLiteOpenHelper { 
        public DatabaseHelper(Context context) { 
            super(context, DATABASE_NAME, null, DATABASE_VERSION); 
            // TODO Auto-generated constructor stub 
        } 
 
        @Override 
        public void onCreate(SQLiteDatabase db) { 
            // TODO Auto-generated method stub 
            db.execSQL(DATABASE_CREATE); 
 
        } 
 
        @Override 
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
            // TODO Auto-generated method stub 
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
            onCreate(db); 
        } 
    } 
 
    private Context mCtx = null; 
    private DatabaseHelper dbHelper; 
    private SQLiteDatabase db; 
 
    public NotesDbAdapter(Context ctx) { 
        this.mCtx = ctx; 
    } 
 
    public NotesDbAdapter open() throws SQLException { 
        dbHelper = new DatabaseHelper(mCtx); 
        db = dbHelper.getWritableDatabase(); 
        return this; 
    } 
 
    public void close() { 
        dbHelper.close(); 
    } 
 
    private static final String KEY_ROWID = "_id"; 
    static final String KEY_NOTE = "note"; 
    private static final String KEY_CREATED = "created"; 
     
    String[] strCols = new String[]{ 
            KEY_ROWID,   
            KEY_NOTE, 
            KEY_CREATED, 
    }; 
 
    // get all entries 
    public Cursor getall() { 
        return db.query(DATABASE_TABLE, //Which table to Select 
                strCols, //Which columns to return 
                null, //WHERE clause 
                null, //WHERE arguments 
                null, //GROUP BY clause 
                null, //HAVING clause 
                null);//Order-by clause 
 
    } 

重構:
由於“”字件符串只會在“”語句中使用一次,因此為了省下記憶體空間,而少聲明一個“”變數,即將“”與“”兩段和寫在一起,在其他程式中碰到次狀況,也可以這樣處理。
修改後的“DummyDbAdapter.java”檔案如下:
[java] 
package com.demo.android.dummynote; 
 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
 
public class NotesDbAdapter { 
    private static final String DATABASE_NAME = "notes.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_TABLE = "notes"; 
    private static final String DATABASE_CREATE = "creat table notes(" 
            + "_id INTEGER PRIMARY KEY," + "note TEXT," + "created INTEGER," 
            + "modified INTEGER" + ");"; 
 
    private static class DatabaseHelper extends SQLiteOpenHelper { 
        public DatabaseHelper(Context context) { 
            super(context, DATABASE_NAME, null, DATABASE_VERSION); 
            // TODO Auto-generated constructor stub 
        } 
 
        @Override 
        public void onCreate(SQLiteDatabase db) { 
            // TODO Auto-generated method stub 
            db.execSQL(DATABASE_CREATE); 
 
        } 
 
        @Override 
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
            // TODO Auto-generated method stub 
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
            onCreate(db); 
        } 
    } 
 
    private Context mCtx = null; 
    private DatabaseHelper dbHelper; 
    private SQLiteDatabase db; 
 
    public NotesDbAdapter(Context ctx) { 
        this.mCtx = ctx; 
    } 
 
    public NotesDbAdapter open() throws SQLException { 
        dbHelper = new DatabaseHelper(mCtx); 
        db = dbHelper.getWritableDatabase(); 
        return this; 
    } 
 
    public void close() { 
        dbHelper.close(); 
    } 
 
    private static final String KEY_ROWID = "_id"; 
    static final String KEY_NOTE = "note"; 
    private static final String KEY_CREATED = "created"; 
     
    // get all entries 
    public Cursor getall() { 
        return db.query(DATABASE_TABLE, //Which table to Select 
                new String[]{KEY_ROWID, KEY_NOTE,KEY_CREATED}, //Which columns to return 
                null, //WHERE clause 
                null, //WHERE arguments 
                null, //GROUP BY clause 
                null, //HAVING clause 
                null);//Order-by clause 
 
    } 


作者:leihengxin

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.