馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
各位大大好,小弟須要寫個小程式,要從現有的資料庫中以下拉式選單選取數值出來畫圖,現在碰到了個問題,資料庫檔案不知道該放在哪及如何引用?在網路上搜尋了很久,找到幾個方法是將資料庫讀出再存到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將檔案推入虛擬機上執行是沒問題的,可以成功讀取指定的欄位,可是安裝到手機上就會跳錯誤.
請問有人知道如何解決這個問題嗎?先謝謝各位大大了,謝謝! |