我正在尝试在我的Android应用程序中创建Sqlite数据库。我编写了以下类,但是当我执行Insert
方法时,它会给出以下错误消息:
onButtonClick,我创建了Database类的对象
DBHelper dbHelper= new DBHelper(getApplicationContext());
dbHelper.insertRemainder(formData);
错误
android.database.sqlite.SQLiteException: no such table: remainders (code 1): ,
while compiling: INSERT INTO remainders(REMAINDER_NOTES,PRIORITY,DATETIME,REMAINDER_NAME)
VALUES (?,?,?,?)
这是我的SqliteDatabaseConnection
班级
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="remainderApp";
private static final String TABLE_NAME= "remainders";
private static final String ID= "ID";
private static final String REMAINDER_NAME= "REMAINDER_NAME";
private static final String REMAINDER_NOTES= "REMAINDER_NOTES";
private static final String DATETIME= "DATETIME";
private static final String PRIORITY = "PRIORITY";
private static final String CREATE_TABLE= " create table "+ TABLE_NAME + " ( "+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+REMAINDER_NAME+" TEXT, "
+ REMAINDER_NOTES +" TEXT, "+DATETIME+" DATE, "+PRIORITY+" TEXT )";
private static final String DROP_TABLE= "DROP TABLE IF EXISTS"+ TABLE_NAME;
private Context context;
private SQLiteDatabase db = null;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null , 1);
this.context= context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
e.printStackTrace();
}
}
//CRUD
public boolean insertRemainder(FormData formData){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(REMAINDER_NAME,formData.getRemainderName());
values.put(REMAINDER_NOTES, formData.getRemainderNotes());
values.put(DATETIME, formData.getDateTime() );
values.put(PRIORITY, formData.getRemainderPriority());
long id= db.insert(TABLE_NAME, null, values);
Log.d("tableQuery", CREATE_TABLE);
if(id > 0){
return true;
}else{
return false;
}
}
当我打印CREATE_TABLE
变量时,它会打印以下查询。
创建表余数(ID INTEGER PRIMARY KEY AUTOINCREMENT, REMAINDER_NAME TEXT,REMAINDER_NOTES TEXT,DATETIME DATE,PRIORITY 文字)