DatabaseHelper类,未调用onCreate()

时间:2013-03-22 10:56:17

标签: java database sqliteopenhelper

我正在主要活动的DBHelper中创建onCreate()类的实例。

 databaseHelper = new DatabaseHelper(getBaseContext());

onCreate()被叫时我没有得到。我试着给Toast。当getWritableDatabase();被称为申请人关闭时。这是我的代码:

public class DatabaseHelper extends SQLiteOpenHelper
{
    SQLiteDatabase db;
    // Database Name
    private static final String DATABASE_NAME = "manasi.db";
    // database table name
    private static final String TABLE_DATA = "data";
    // Database table fields
    private static final String KEY_IMEI = "imei";
    private static final String KEY_LAT = "lat";
    private static final String KEY_LONG = "long";
    private static final String KEY_DATETIME = "datetime";
    private static final String KEY_ALTITUDE = "altitude";
    private static final String KEY_SPEED = "speed";
    private static final String KEY_BATTERY = "battery";
    Context context;
    /**
     * @param context Application Context
     */
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        // db = this.getWritableDatabase();
        Toast.makeText(context, "db helper", Toast.LENGTH_LONG).show();
    }
    /* Called when the database is created for the first time.
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE DB_TABLE(_id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT);");
        Toast.makeText(context, "db helper onCreate", Toast.LENGTH_LONG).show();
        String CREATE_DATA_TABLE = "CREATE TABLE " + TABLE_DATA + "("
                + KEY_IMEI + " TEXT ," + KEY_LAT + " DOUBLE," + KEY_LONG
                + "DOUBLE," + KEY_DATETIME + "DATETIME," + KEY_ALTITUDE + "DOUBLE,"
                + KEY_SPEED + "TEXT," + KEY_BATTERY + "TEXT" + ")";
        db.execSQL(CREATE_DATA_TABLE);
        Context context = null;

    }

    /* Called when the database needs to be upgraded.
    * @see    android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
    */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("DatabaseHelper", "Upgrading database, which will destroy all old data");
        onCreate(db);
    }

    public void insertRecordToDB(Data data) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_IMEI, data.getImei());
        values.put(KEY_LAT, data.getLatitude());
        values.put(KEY_LONG, data.getLongitude());
        values.put(KEY_DATETIME, data.getDateTime());
        values.put(KEY_ALTITUDE, data.getAltitude());
        values.put(KEY_SPEED, data.getSpeed());
        values.put(KEY_BATTERY, data.getBattery());
        // Inserting Row
        db.insert(TABLE_DATA, null, values);
        db.close(); // Closing database connection*/
    }


}

1 个答案:

答案 0 :(得分:3)

在您的活动中执行此操作

databaseHelper = new DatabaseHelper(this);
SQLiteDatabase sb = databaseHelper.getWritableDatabase();//this line responsible to call onCreate()

并阅读此http://mobileapplications-by-himanshu.blogspot.in/

onCreate()调用一次。