问题描述
- 创建数据库和默认数据
-
在SQlite数据库中插入默认数据代码如下:
private static final String DATABASE_CREATE_position = "CREATE TABLE position(latitude VARCHAR(20), longitude VARCHAR(20), address VARCHAR(100))"; @Override public void onCreate(SQLiteDatabase db) { // Create the table db.execSQL(DATABASE_CREATE_position); ContentValues defaultPos = new ContentValues(); defaultPos.put("latitude", "40.604398"); defaultPos.put("longitude", "-3.709002"); defaultPos.put("address", "Avenida del parque "); db.insert("position", null, defaultPos); db.close(); }
结果运行之后数据库是空的,请帮忙看一下问题出在哪里了?谢谢
解决方案
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_position);
db.execSQL("insert into position (latitude,longitude,address) values (40.604398,-3.709002,'Avenida del parque ')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
....
解决方案二:
don't close database in onCreate.
时间: 2024-09-12 04:59:01