2012-04-07 58 views
0

我正在爲在線考試設計一個應用程序。我使用SQLite Browser創建了一個數據庫並將其提交給Eclipse。在模擬器中工作正常;它能夠檢索和存儲數據。但是,當我將.apk文件放在手機上時,問題就出現了。在移動設備上無法檢索現有的數據庫。我無法將數據庫文件與.apk文件綁定,即使將其放入資源文件夾後也是如此。Android數據庫

任何人都可以幫忙嗎?

詳情:

  1. 註冊模塊
  2. 用戶測試模塊(顯示從數據庫中的問題)
  3. 分數提交模塊

回答

0

你是如何在數據庫查詢?

正確的方法不是在代碼中放置單獨的數據庫,而是動態創建一個數據庫。對於例如以下代碼:

private static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DB_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSql("/*Put Create Table sqls here*/"); 
     //onCreate will be called only once(when db doesn't exists for application, it creates here with the code) 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 
} 

在代碼中使用這樣的數據庫輔助類。

現在,只要你想在你的數據庫查詢,你可以做這樣的:

dbHelper = new DatabaseHelper(ctx); 
    db = dbHelper.getWritableDatabase(); 
    //Start querying on db.(if it is not created oncreate() of dbhelper will create it for you. 

所以,簡單地把你的初始數據庫創建OnCreate中)dbhelper

的,/ insert語句(