gpt4 book ai didi

java - DatabaseHelper 类,onCreate() 未调用

转载 作者:行者123 更新时间:2023-12-01 23:56:14 25 4
gpt4 key购买 nike

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

 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*/
}


}

最佳答案

在您的 Activity 中执行此操作

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

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

onCreate() 调用一次。

关于java - DatabaseHelper 类,onCreate() 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15568592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com