2016-03-08 62 views
0

我有數據庫模式:Android應用程序沒有創建數據庫

public class Database extends SQLiteOpenHelper { 

    public static final String TAG = Database.class.getSimpleName(); 

    public static final String DATABASE_NAME = "Database"; 
    public static final int DATABASE_VERSION = 1; 

    SaveSharedPreference preference = new SaveSharedPreference(); 


    public Database(Context context) { 
     super(context, Environment.getExternalStorageDirectory() + "/sdcard/" + DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    static SQLiteDatabase database = null; 
    static Database instance = null; 

    public static SQLiteDatabase getDatabase() { 
     if (null == database) { 
      database = instance.getWritableDatabase(); 
     } 
     return database; 
    } 

    public static void init(Context context) { 
     if (null == instance) { 
      instance = new Database(context); 
     } 
    } 

    public static void deactivate() { 
     if (null != database && database.isOpen()) { 
      database.close(); 
     } 
     database = null; 
     instance = null; 
    } 

    // NON-CUSTOM COLUMNS 
    public static final String _ID = "id"; 
    public static final String CREATE_DT = "create_dt"; 
    public static final String UPDATE_DT = "update_dt"; 

    // EVENT TABLE 
    public static final String TABLE_NAME_EVENTS = "events"; 
    public static final String EVENT_SYNC_ID = "event_sync_id"; 
    public static final String EVENT_CREATOR_SYNC_ID = "event_creator_sync_id"; 
    public static final String EVENT_PREFERENCES = "event_preferences"; 


    public static final String TABLE_EVENTS_CREATE = "CREATE TABLE " 
      + TABLE_NAME_EVENTS + " (" 
      + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + EVENT_SYNC_ID + " VARCHAR(255) NOT NULL, " 
      + EVENT_CREATOR_SYNC_ID + " VARCHAR(255) NOT NULL, " 
      + EVENT_PREFERENCES + " VARCHAR(8000) NOT NULL, " 
      + CREATE_DT + " VARCHAR(255), " 
      + UPDATE_DT + " VARCHAR(255))"; 

    // DROPPING TABLE: Should do instead of truncate if need to refresh all data/ 
    public static final String DROP_TABLE_PREFIX = "DROP TABLE IF EXISTS "; 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     // CREATE TABLES DEFINED ABOVE 
     db.execSQL(DROP_TABLE_PREFIX + TABLE_NAME_EVENTS); 

     onCreate(db); 
    } 

和MyApplicaton類,來創建應用程序啓動數據庫:

public void onCreate() { 
    super.onCreate(); 

    Database DB = new Database(this); 
    SQLiteDatabase db = DB.getWritableDatabase(); 

} 

我已經使用上述代碼試圖,但看着文件路徑/sdcard/,沒有創建數據庫。

這個數據庫如何被創建並確保它被創建?

+0

你打算在設備或模擬器上看到你的數據庫? – malli

+0

我在'Database'類的'onCreate()'方法中看不到'TABLE_EVENTS_CREATE'的'execSQL()'。它是否存在於你的實際代碼中?沒有它,數據庫將不會被創建。 –

回答

1
You can only create database in onCreate() Method 

@Override 
    public void onCreate(SQLiteDatabase db) { 

     // CREATE TABLES DEFINED ABOVE 
     db.execSQL(TABLE_NAME_EVENTS); 

     onCreate(db); 
    } 

for Dropping table use 

@Override 
    public void onUpgrade(SQLiteDatabase db) { 

     // DroppingTABLES DEFINED ABOVE 
     db.execSQL(DROP_TABLE_PREFIX); 

     onCreate(db); 
    } 
相關問題