2013-02-05 67 views
0

這基本上是我的第一個android應用程序,我一直試圖使用預填充的sqllite數據庫來解決我的需求。onCreate方法不會在DatabaseHandler中調用擴展SQLiteOpenHelper

我的問題是,onCreate函數沒有被調用。

我已經從很多地方採集了一些代碼,並將它們組合起來形成這個類。

Updating prepopulated database in Android

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

和其他一些人從android.com

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Locale; 
import java.util.Random; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DatabaseHandler extends SQLiteOpenHelper { 

    SQLiteDatabase db; 
    private static final String TAG = "DataBaseHelper"; 
    int id = 0; 
    Random random = new Random(); 
    private SQLiteDatabase myDataBase; 
    private final Context myContext; 

    // Constructor 
    public DatabaseHandler(Context context) { 
     super(context, DefinitionContract.DATABASE_NAME, null, DefinitionContract.DATABASE_VERSION); 
     Log.d(TAG, "DatabaseHandler constructor called\n"); 
     db = getWritableDatabase(); 
     this.myContext = context; 
     //createDB(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db){ 
     Log.d(TAG, "onCreate called\n"); 
     createDB(); 
    } 

    // Upgrading database 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.d(TAG, "onUpgrade called\n"); 
     Log.w(TAG, "Upgrading DB from version " + oldVersion + " to " + 
        newVersion + ", which will destroy all old data"); 

     // Drop older table if existed 
     String sql = "DROP TABLE IF EXISTS " + DefinitionContract.CATEGORY_TABLE_NAME; 
     db.execSQL(sql); 

     sql = "DROP TABLE IF EXISTS " + DefinitionContract.CONTENTS_TABLE_NAME; 
     db.execSQL(sql); 

     // Create table again 
     onCreate(db); 
    } 

    public void createDataBase(SQLiteDatabase db) { 
     Log.d(TAG, "createDataBase called\n"); 
     createDB(); 
     db.execSQL(DefinitionContract.CREATE_CATEGORY_TABLE); 
     db.execSQL(DefinitionContract.CREATE_CONTENTS_TABLE); 
    } 

    private void createDB() { 
     Log.d(TAG, "createDB called\n"); 
     boolean dbExist = dbExists(); 
     Log.d("SQL Helper", "Condition:\n"); 
     if(!dbExist) { 
      Log.d("SQL Helper", "Condition 1\n"); 
      copyDataBase(); 
     } else if(dbExist) { 
      Log.d("SQL Helper", "Condition 2\n"); 
      copyDataBase(); 
     } 
    } 

    private boolean dbExists() { 
     Log.d(TAG, "dbExists called\n"); 
     //File dbFile = new File(DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME); 
     //return dbFile.exists(); 

     SQLiteDatabase db = null; 
     try { 
      String dbPath = DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME; 
      db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE); 
      db.setLocale(Locale.getDefault()); 
      db.setLockingEnabled(true); 
      db.setVersion(DefinitionContract.DATABASE_VERSION); 
     } 
     catch(SQLiteException e){ 
      Log.e("SQL Helper", "database not found"); 
     } 
     if(db != null) { 
      db.close(); 
     } 
     return db != null ? true : false; 
    } 

    private void copyDataBase() { 
     Log.d(TAG, "copyDataBase called \n"); 
     InputStream iStream = null; 
     OutputStream oStream = null; 
     String outFilePath = DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME; 
     try{ 
      iStream = myContext.getAssets().open(DefinitionContract.DATABASE_NAME_EXT); 
      oStream = new FileOutputStream(outFilePath); 
      byte[] buffer = new byte[1024]; 
      int length; 
      while((length = iStream.read(buffer))>0) { 
       oStream.write(buffer,0,length); 
      } 
      oStream.flush(); 
      oStream.close(); 
      iStream.close(); 
     } 
     catch(IOException ioe){ 
      throw new Error("Problem copying database from resource file."); 
     } 
    } 

    public void openDataBase() throws SQLException { 
     String myPath = DefinitionContract.DATABASE_PATH + DefinitionContract.DATABASE_NAME_EXT; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
    } 

    @Override 
    public synchronized void close() { 
     Log.d(TAG, "close called\n"); 
     if (myDataBase != null) 
      myDataBase.close(); 
     super.close(); 
    } 

    public void readDB() { 
     Log.d(TAG, "readDB called\n"); 
     String selectQuery = "SELECT * FROM " + DefinitionContract.DATABASE_ONLYNAME+"."+DefinitionContract.CATEGORY_TABLE_NAME; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       int arraySize = cursor.getColumnCount(); 
       String newlog = "arraySize=" + arraySize + "*****"; 
       for(int i = 0; i < arraySize; i++) { 
        newlog = newlog + cursor.getString(i)+ "\n"; 
       } 

       Log.d("Details: ", newlog); 
      } while (cursor.moveToNext()); 
     } 
    } 

} 

我已經做了谷歌搜索,並試圖修復它,但它並沒有正常工作。

Android SQLiteOpenHelper : onCreate() method is not called. Why?

SQLiteOpenHelper failing to call onCreate?

logcat的showns只有 「數據庫處理器構造叫做」,然後 「關閉虛擬機」 後

但如果我刪除分貝= getWritableDatabase();行來自我的構造函數它進一步工作,並顯示正在調用readDB()函數。

一些定義可能會理解我的代碼幫助:

public static final String DATABASE_NAME  = "mydb.db"; 

public static final String DATABASE_NAME_EXT  = "mydb.sqllite"; 

// Database path 
public static final String DATABASE_PATH = "/data/data/com.abhishekgdotcom.collection/databases/"; 

任何幫助的傢伙?

回答

1

那麼,爲什麼onCreate()方法不適用於我,因爲它僅在應用程序第一次運行時才被調用。或者當您更改DATABASE_VERSION時,則會調用onUpgrade,然後再調用onCreate方法。

我相信這是默認的android工作。

P.S我可以通過更改數據庫版本或從/data/data/packagename/databases/

0

我會推薦你​​另一個代碼組織,因爲你在同一個類中做了許多不相關的事情。通常,一個好的設計是定義一個從SQLiteHelper繼承的OpenHelper類,它只是創建或更新數據庫。然後,從另一個外部類(通常來自您的活動),實例化該類,然後從那裏調用getWritableDatabase()。也許你會很容易找到bug。

+0

刪除數據庫文件獲取工作的onCreate每次所以基本上這個想法是從活動調用getReadableDatabase()。我是否也應該通過活動課的查詢? – abhig10

+0

或多或少,我通常在我的答案中定義一個OpenHelper類,以及一個檢索getWritableDatabase()並進行所有查詢的DataManager類。我將DataManager的一個實例存儲在我的Application實例中(在它的onCreate方法中),因此它可以被我所有的活動共享(避免併發問題的最佳方法)。從活動中,我從我的應用程序實例中檢索DataManager實例,然後通過它的方法進行查詢。只有DataManager使用OpenHelper,沒有其他人。 – thelawnmowerman

+0

我的錯誤似乎已解決,但現在彈出另一個錯誤說'(5)數據庫已被鎖定。任何想法爲什麼? – abhig10

相關問題