我正在尝试为我的Android应用程序创建一个数据库,但它似乎没有创建。这是我的代码:
public class PersonDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "data.sqlite";
private static final int VERSION = 1;
private static final String TABLE_PERSON = "person";
private static final String COLUMN_PERSON_NAME = "name";
public PersonDatabaseHelper(Context context) {
super(context, DB_NAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person ("
+ "id integer primary key autoincrement "
+ "name varchar(100))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
我这样打电话给我的帮手:
new PersonDatabaseHelper(getActivity().getApplicationContext());
当我检查我的数据库是这样的:
private boolean databaseExists(){
File database=getActivity().getApplicationContext().getDatabasePath("data.sqlite");
if (!database.exists()) {
Log.i("Database", "Not Found");
return false;
} else {
Log.i("Database", "Found");
return true;
}
}
我总是收到找不到的数据库。我在这做错了什么? :)
答案 0 :(得分:3)
数据库将在第一次访问时创建,例如在getReadableDatabase()
个实例上调用getWritableDatabase()
或SQLiteOpenHelper
。