0

我正在主要活动中创建一个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*/
    }


}
4

1 回答 1

3

在您的活动中执行此操作

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

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

onCreate() 调用一次。

于 2013-03-27T10:13:25.743 回答