I'm confused, I already added a column named "TIME" in my DatabaseHelper.java
as you can see in the code below, and even double-checked it. I synced my gradle files, invalidate caches and restart, and clean and rebuild project, but still get this error in Logcat
:
Logcat Error
android.database.sqlite.SQLiteException: table Student_table has no column named TIME (code 1 SQLITE_ERROR[1]): , while compiling: INSERT INTO Student_table(SURNAME,ID,DATE,NAME,TIME) VALUES (?,?,?,?,?)
DatabaseHelper.java
package com.example.AppDraft2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatabaseHelper extends SQLiteOpenHelper {
private Context context;
public static final String DATABASE_NAME="Student.db";
public static final String TABLE_NAME="Student_table";
public static final String COL_1="DATE";
public static final String COL_2="TIME";
public static final String COL_3="NAME";
public static final String COL_4="SURNAME";
public static final String COL_5="ID";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "( DATE TEXT, TIME TEXT, NAME TEXT, SURNAME TEXT, ID INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public boolean insertData(String name,String surname,String id){
// set the format to sql date time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
Date time = new Date();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, dateFormat.format(date));
contentValues.put(COL_2, timeFormat.format(time));
contentValues.put(COL_3, name);
contentValues.put(COL_4, surname);
contentValues.put(COL_5, id);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result==-1){
return false;
}else{
return true;
}
}
public Cursor getAllData(){
//get all data
SQLiteDatabase db=this.getWritableDatabase();
Cursor res = db.rawQuery("select*from " + TABLE_NAME, null);
return res;
}
public Integer deleteData(){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(TABLE_NAME,null,null);
}
}