为什么我的Notes表没有创建,任何人都可以在我的查询字符串中看到任何错误。我已经发现我没有将数据输入我的笔记表,因为它不存在。
private static final String CREATE_NOTES = "CREATE TABLE "
+ NOTE_TABLE + "("
+ NOTES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ FK_ID + " INTEGER NOT NULL, "
+ COLUMN_DATE + " DATETIME DEFAULT CURRENT_TIMESTAMP,"
+ COLUMN_TITLE + " TEXT NOT NULL, "
+ COLUMN_BODY + " TEXT, "
+ " FOREIGN KEY ("+FK_ID+") REFERENCES "+USER_TABLE+" ("+COLUMN_ID+"));";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
//Deletes database
// context.deleteDatabase(DATABASE_NAME);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USERS);
db.execSQL(CREATE_NOTES);
}
答案 0 :(得分:0)
执行以下示例 -
public class DatabaseHelperextends SQLiteOpenHelper{
private static DatabaseHelper mDatabaseHelper;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
//Deletes database
// context.deleteDatabase(DATABASE_NAME);
}
public static DatabaseHelper getInstance(Context context){
if(mDatabaseHelper==null){
mDatabaseHelper= new DatabaseHelper(context);
}
return mDatabaseHelper;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USERS);
db.execSQL(CREATE_NOTES);
}
}
并在insert语句之前调用getInstance()方法。
DatabaseHelper mHelper = DatabaseHelper.getInstance() ;
mHelper.insert();
如果不存在,它将创建新的数据库。