Android 台灣中文網

標題: 如果只是讀取sqlite資料庫 須要用到SD卡儲存嗎? [打印本頁]

作者: dio50    時間: 2012-10-26 19:11
標題: 如果只是讀取sqlite資料庫 須要用到SD卡儲存嗎?
各位大大好,小弟須要寫個小程式,要從現有的資料庫中以下拉式選單選取數值出來畫圖,現在碰到了個問題,資料庫檔案不知道該放在哪及如何引用?在網路上搜尋了很久,找到幾個方法是將資料庫讀出再存到SD卡中,但是我只需要讀取資料就好,不必寫入資料庫.以下是我的原始碼:

public class DatabaseHandler extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name 我想問題可能在這,小弟不知是否可以在這指定路徑直接讀取資料庫檔案?
    private static final String DATABASE_NAME = "sat.db";

    // Labels table name 由於沒指定路徑 所以再預設的資料夾下找不到檔案(或是系統建了個空資料庫?)以致讀不掉資料
    private static final String TABLE_LABELS = "area_name";

    // Labels Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
   

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

        // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
            // Category table create query
//            String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
//                        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
//            db.execSQL(CREATE_CATEGORIES_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        //db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

        // Create tables again
        //onCreate(db);
    }
   
    /**
     * Inserting new lable into lables table
     * */
    public void insertLabel(String label){
//     SQLiteDatabase db = this.getWritableDatabase();
//           
//            ContentValues values = new ContentValues();
//            values.put(KEY_NAME, label);
//             
//            // Inserting Row
//        db.insert(TABLE_LABELS, null, values);
//        db.close(); // Closing database connection
    }
   
    /**
     * Getting all labels
     * returns list of labels
     * */
    public List<String> getAllLabels(){
            List<String> labels = new ArrayList<String>();
           
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_LABELS;
     
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
     
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                    labels.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }
        
        // closing connection
        cursor.close();
        db.close();
           
            // returning lables
            return labels;
    }
}

以上原始碼是從網上"參考"來的,有些功能小弟沒用到就先註解掉了.
小弟用eclipse將檔案推入虛擬機上執行是沒問題的,可以成功讀取指定的欄位,可是安裝到手機上就會跳錯誤.
請問有人知道如何解決這個問題嗎?先謝謝各位大大了,謝謝!
作者: ploglin    時間: 2012-10-29 12:33
如果你使用的是 SQLiteOpenHelper 那就不需要存取 SD CARD 的權限唷

請參考官網的說明 http://developer.android.com/guide/topics/data/data-storage.html#db
作者: arica    時間: 2012-11-8 11:33
以上寫法的資料庫檔案是放在 internal storage ( 非 sdcard )
所以跟 SD 應該沒關係
而您的 DATABASE_NAME 只能影響檔案 "名稱" ( 檔案路徑是內定的 )




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1