-1

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);
    }

}


forpas
  • 160,666
  • 10
  • 38
  • 76
MRPenguin
  • 19
  • 4

1 Answers1

1

Regarding you comment (in the now deleted Answer)

I already tried uninstalling the app and installing it many times but still nothing happens

I believe that uninstalling the App and then rerunning the App is the fix to the issue (however, perhaps quickly have a look at the end of this answer in regards to the Manifest). That is on the device on which you are running the App go into settings list the Apps and uninstall the App.

However, as you appear convinced that this is not the fix. Then perhaps try amending the insertData method to temporarily include the following code immediately after the line SQLiteDatabase db = this.getWritableDatabase(); :-

    //<<<<<<<<<< ADDED FOR TESTING
    Cursor csr = db.query("sqlite_master",new String[]{"sql"},"name='" + TABLE_NAME + "'",null,null,null,null);
    DatabaseUtils.dumpCursor(csr);
    csr.close();
    //<<<<<<<<<<<
  • This extracts the SQL that is used to create the table (as per TABLE_NAME) and dump the cursor to the Log.

Testing Stage 1

Using your DatabaseHelper with the above modification BUT purposefully using:-

db.execSQL("create table " + TABLE_NAME + "( DATE TEXT,  NAME TEXT, SURNAME TEXT, ID INTEGER)");
  • i.e. NO TIME COLUMN

The running the testing App using the following in an activity :-

    DatabaseHelper db2 = new DatabaseHelper(this);
    db2.insertData("Fred","Bloggs","FB001");

Results in :-

2021-04-03 08:52:18.439 2388-2388/a.a.so66891171javaroomemojii I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@bc1ea7c
2021-04-03 08:52:18.441 2388-2388/a.a.so66891171javaroomemojii I/System.out: 0 {
2021-04-03 08:52:18.441 2388-2388/a.a.so66891171javaroomemojii I/System.out:    sql=CREATE TABLE Student_table( DATE TEXT,  NAME TEXT, SURNAME TEXT, ID INTEGER)
2021-04-03 08:52:18.441 2388-2388/a.a.so66891171javaroomemojii I/System.out: }
2021-04-03 08:52:18.441 2388-2388/a.a.so66891171javaroomemojii I/System.out: <<<<<
2021-04-03 08:52:18.442 2388-2388/a.a.so66891171javaroomemojii E/SQLiteLog: (1) table Student_table has no column named TIME
2021-04-03 08:52:18.445 2388-2388/a.a.so66891171javaroomemojii E/SQLiteDatabase: Error inserting DATE=2021-04-03 TIME=08:52:18 SURNAME=Bloggs ID=FB001 NAME=Fred
    android.database.sqlite.SQLiteException: table Student_table has no column named TIME (code 1 SQLITE_ERROR): , while compiling: INSERT INTO Student_table(DATE,TIME,SURNAME,ID,NAME) VALUES (?,?,?,?,?)

i.e you can clearly see that the message/failure is correct there is no TIME column.

Testing Stage 2

Just changing DatabaseHelper to use :-

db.execSQL("create table " + TABLE_NAME + "( DATE TEXT, TIME TEXT, NAME TEXT, SURNAME TEXT, ID INTEGER)");

and rerunning without uninstalling the App results in :-

2021-04-03 08:56:00.155 2621-2621/a.a.so66891171javaroomemojii I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@bc1ea7c
2021-04-03 08:56:00.155 2621-2621/a.a.so66891171javaroomemojii I/System.out: 0 {
2021-04-03 08:56:00.156 2621-2621/a.a.so66891171javaroomemojii I/System.out:    sql=CREATE TABLE Student_table( DATE TEXT,  NAME TEXT, SURNAME TEXT, ID INTEGER)
2021-04-03 08:56:00.156 2621-2621/a.a.so66891171javaroomemojii I/System.out: }
2021-04-03 08:56:00.156 2621-2621/a.a.so66891171javaroomemojii I/System.out: <<<<<
2021-04-03 08:56:00.156 2621-2621/a.a.so66891171javaroomemojii E/SQLiteLog: (1) table Student_table has no column named TIME
2021-04-03 08:56:00.160 2621-2621/a.a.so66891171javaroomemojii E/SQLiteDatabase: Error inserting DATE=2021-04-03 TIME=08:56:00 SURNAME=Bloggs ID=FB001 NAME=Fred
    android.database.sqlite.SQLiteException: table Student_table has no column named TIME (code 1 SQLITE_ERROR): , while compiling: INSERT INTO Student_table(DATE,TIME,SURNAME,ID,NAME) VALUES (?,?,?,?,?)
  • i.e. the same error BUT different times.

Testing Stage 3

Without making any changes and just uninstalling the App :-

2021-04-03 08:58:28.436 2949-2949/a.a.so66891171javaroomemojii I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@bc1ea7c
2021-04-03 08:58:28.437 2949-2949/a.a.so66891171javaroomemojii I/System.out: 0 {
2021-04-03 08:58:28.438 2949-2949/a.a.so66891171javaroomemojii I/System.out:    sql=CREATE TABLE Student_table( DATE TEXT, TIME TEXT, NAME TEXT, SURNAME TEXT, ID INTEGER)
2021-04-03 08:58:28.438 2949-2949/a.a.so66891171javaroomemojii I/System.out: }
2021-04-03 08:58:28.438 2949-2949/a.a.so66891171javaroomemojii I/System.out: <<<<<
2021-04-03 08:58:28.453 2949-2949/a.a.so66891171javaroomemojii D/OpenGLRenderer: Skia GL Pipeline
  • i.e. no exception and the TIME column now exists

The Manifest (AndroidManifest.xml) If you follow the above and still have an issue then in some cases it may be the data being restored in which case uninstall the App and then try running with the Manifest changed to have :-

android:allowBackup="false"
  • instead of android:allowBackup="true"
MikeT
  • 51,415
  • 16
  • 49
  • 68