0

I have been following an android tutorial to implement SQLite Database to register users. was able to create database however, when my app tries to verify the username( if the username has already been taken ) It throws an error which goes

FATAL EXCEPTION: main
                  Process: com.example.pc.codedelta, PID: 5617
                  android.database.sqlite.SQLiteException: no such column: USERNAME (code 1): , while compiling: SELECT * FROM people WHERE  USERNAME=?
                      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.<init>(SQLiteProgram.java:58)
                      at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                      at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                      at com.example.pc.codedelta.RegisterActivity.Checkingusername(RegisterActivity.java:117)
                      at com.example.pc.codedelta.RegisterActivity$1.onClick(RegisterActivity.java:56)
                      at android.view.View.performClick(View.java:6256)
                      at android.view.View$PerformClick.run(View.java:24697)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.

This is the point where my program crashes at: (crashes at line starting with cursor)

public void Checkingusername()
{

        sqLiteDatabaseObj = sqLiteHelper.getWritableDatabase();
        cursor = sqLiteDatabaseObj.query(sqLiteHelper.TABLE_NAME,null," "+
                sqLiteHelper.COLUMN_USERNAME +"=?", new String[]{username},null,null,null,null);


        while(cursor.moveToNext()){
            if(cursor.isFirst()){
                cursor.moveToFirst();
                Result = "Found";

                cursor.close();
            }
        }
        CheckFinal();
    }

my SQLiteHelper.java file

public class SQLiteHelper extends SQLiteOpenHelper
{
    static String DATABASE_NAME ="tutorapp.db";
    public static final String TABLE_NAME = "people";
    public static final String TABLE_COL_ID="id";
    public static final String COLUMN_NAME ="name";

    public static final String COLUMN_PHONE ="PHONE";
    public static final String COLUMN_D0B = "DOB";
    public static final String COLUMN_USERNAME = "USERNAME";
    public static final String COLUMN_PASSWORD = "PASSWORD";

    private static final int DATABASE_VERSION = 1;

    public SQLiteHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        String SQL_CREATE_TABLE ="CREATE TABLE " +
                TABLE_NAME + "(" + TABLE_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " VARCHAR, " +

                COLUMN_D0B + " VARCHAR, " +
                COLUMN_PHONE + " VARCHAR, " +
                COLUMN_USERNAME + " VARCHAR, "+
                COLUMN_PASSWORD + " VARCHAR)";

        sqLiteDatabase.execSQL(SQL_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }
}

Any Help would be highly appreciated! Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
aidray
  • 1
  • 1
  • You added the column after a previous app run. See: [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – Phantômaxx Nov 27 '17 at 09:52
  • You have 3 simple options *(basically `onCreate` is only called automatically once for the lifetime of the database, so you need to delete the database or drop or alter the table to add a column)*. **a**) Delete/Clear the App's data, **b**) uninstall the App or **c**) increase the value of DATABASE_VERSION (to 2). (c works as `onUpgrade` which will drop the table and then call `onCreate`). – MikeT Nov 27 '17 at 09:59

0 Answers0