0

I have created a table BookDb but the query to create the columns is not getting executed in the onCreate() method of my DbHelper class which is extension of SQLiteOpenHelper().

Here is the class DbHlelper:

public class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, Contract.FeedEntry.TABLE_NAME, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_ENTRIES =
                "CREATE TABLE IF NOT CREATED BookDb( id INTEGER PRIMARY KEY, " +
                        "BookName TEXT, " +
                        "Author TEXT, " +
                        "IssuedOn TEXT, " +
                        "DueDate TEXT, " +
                        "Fine TEXT, " +
                        "Totalfine TEXT )" ;
        db.execSQL(SQL_CREATE_ENTRIES);
        Log.v("Create","TABLE CREATED!!!!!!!!!!");
    }

Here is the code in main activity where I insert values:

void SaveEntry() {
    DbHelper db = new DbHelper(this);

    data = db.getWritableDatabase();
    db.onOpen(data);
    //db.onCreate(data);

    ContentValues values = new ContentValues();
    values.put(Contract.FeedEntry.COLUMN_NAME_Bname, bname);
    values.put(Contract.FeedEntry.COLUMN_NAME_AUTHOR, authname);
    values.put(Contract.FeedEntry.COLUMN_NAME_Issue, issuedate);
    values.put(Contract.FeedEntry.COLUMN_NAME_DUE, dueafter);
    values.put(Contract.FeedEntry.COLUMN_NAME_FINE_PER_DAY, fine);

    long rowId = data.insert(Contract.FeedEntry.TABLE_NAME, null, values);
}

Here is the contract class :

public class Contract {

    private Contract() {}

    /* Inner class that defines the table contents */
    public static class FeedEntry implements BaseColumns {
        public static final String TABLE_NAME = "BookDb";
        public static final String COLUMN_NAME_Bname = "BookName";
        public static final String COLUMN_NAME_AUTHOR="Author";
        public static final String COLUMN_NAME_Issue = "IssuedOn";
        public static final String COLUMN_NAME_DUE="DueDate";
        public static final String COLUMN_NAME_FINE_PER_DAY="Fine";
        public  static final String COLUMN_NAME_TFINE="TotalFine";
    }
}

The log shows::

E/SQLiteLog: (1) table BookDb has no column named Fine E/SQLiteDatabase: Error inserting Fine=nc IssuedOn=vz Author=jx DueDate=bz BookName=va android.database.sqlite.SQLiteException: table BookDb has no column named Fine (code 1): , while compiling: INSERT INTO BookDb(Fine,IssuedOn,Author,DueDate,BookName) VALUES (?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.example.shaily.librarian.AddBook.SaveEntry(AddBook.java:219) at com.example.shaily.librarian.AddBook.onClick(AddBook.java:91) at android.view.View.performClick(View.java:4791) at android.view.View$PerformClick.run(View.java:19884) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5268) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • The most often cause of columns not existing when changes have been made is a misundrstanding of when `onCreate` runs. It does not run every time the SQLIteOpenHelper is instantiated. It should be considered as running ONCE when the database file itself is created (the first open when getReadableDatabase or getWriteableDatabase or an equivalent runs). The quickest fix is to either uninstall the App or to delete the App's Data. This results in the DB being deleted and thus `onCreate` will then be called. – MikeT Jul 04 '17 at 20:57
  • Thanks a lot ! Solvd my problem :) – Shaily Tyagi Jul 05 '17 at 11:51

0 Answers0