我创建了一个SQLiteOpenHelper Class的子类(DatabaseHelper)。它有一个构造函数和两个重写方法
public DatabaseHelper(Context context) {
super(context, dbName, null,33);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE "+ quizTable +"("+quizCol1+" INTEGER PRIMARY KEY,"+
quizCol2+" TEXT,"+ quizCol3 +" TEXT,"+ quizCol4 +" INTEGER," + quizCol5 + " DATETIME )");
db.execSQL("CREATE TABLE "+ questionTable +"("+questionCol1+" INTEGER PRIMARY KEY,"+
questionCol2+" INTEGER,"+ questionCol3 +" TEXT,"+ questionCol4 +" TEXT," + questionCol5 + " TEXT," +
questionCol6 + " TEXT," + questionCol7 +" TEXT," + questionCol8 +" TEXT)");
db.execSQL("CREATE TABLE "+ playedQuizzes +"("+playedQuizCol1+" INTEGER PRIMARY KEY,"+
playedQuizCol2+" TEXT,"+ playedQuizCol3 +" TEXT,"+ playedQuizCol4 +" TEXT," +
playedQuizCol5 + " TEXT)");
System.out.println("this is onCreate method");
InsertQuiz();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+questionTable);
db.execSQL("DROP TABLE IF EXISTS "+playedQuizzes);
onCreate(db);
}
在我的Activity子类中,我使用
调用了上面类的构造函数 public void onStart(){
try{
super.onStart();
dbHelper = new DatabaseHelper(this);
}catch(Exception ex){
CatchError(ex.toString());
}
}
但是当我运行我的Activity类并打开我的DDMS视角时,我没有找到在/ data / data /
下创建的任何数据库我的知识或代码有问题吗?如果是,请告诉我。
答案 0 :(得分:1)
在您的代码中,您没有打开数据库....
创建,编写或打开数据库getWritableDatabase()
或创建&打开数据库getReadableDatabase()
是必要的.....
答案 1 :(得分:0)
来自SQLiteOpenHelper课程的文档:
创建辅助对象以创建,打开和/或管理数据库。这个 方法总是很快返回。实际上并不是数据库 创建或打开,直到getWritableDatabase()之一或 调用getReadableDatabase()。
因此,在不调用getWritableDatabase()或getReadableDatabase()之前,不会创建数据库。