I am trying to create Sqlite database in my android application. I have written following class, but when I execute Insert
Method, it gives me following error message:
onButtonClick, I create object of Database class
DBHelper dbHelper= new DBHelper(getApplicationContext());
dbHelper.insertRemainder(formData);
Error
android.database.sqlite.SQLiteException: no such table: remainders (code 1): ,
while compiling: INSERT INTO remainders(REMAINDER_NOTES,PRIORITY,DATETIME,REMAINDER_NAME)
VALUES (?,?,?,?)
This is my SqliteDatabaseConnection
Class
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;
}
}
When i print the CREATE_TABLE
variable, it prints following query.
create table remainders ( ID INTEGER PRIMARY KEY AUTOINCREMENT, REMAINDER_NAME TEXT, REMAINDER_NOTES TEXT, DATETIME DATE, PRIORITY TEXT )