-1

I am trying to create a table and add users to it when the register and then once the have registered i am allowing them to login im getting an error. the app is crashing on when i hit either the login button or register button but i think the problem is with the database helper class. Here is the error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ie.wit.budget, PID: 3915
                  android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: SELECT username, password from user
                      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.rawQuery(SQLiteDatabase.java:1257)
                      at ie.wit.SQL.DatabaseHelper.checkUser(DatabaseHelper.java:72)
                      at ie.wit.budget.LoginActivity$1.onClick(LoginActivity.java:39)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

DatabaseHelper Class:

public class DatabaseHelper  extends SQLiteOpenHelper{

    // Database Version
    private static final int DATABASE_VERSION = 1;

    //Database Name
    private static final String DATABASE_NAME = "User.db";

    //Table Created
    private static final String TABLE_USER = "user";

    //Columns in Table
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_PASS = "password";
    SQLiteDatabase db;

    private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_USERNAME + " TEXT," +
            COLUMN_EMAIL + " TEXT," + COLUMN_PASS + " TEXT " + ")";

    private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;

    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_USER_TABLE);
        this.db = db;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       db.execSQL(DROP_USER_TABLE);
        this.onCreate(db);

    }

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

    public void insertUser(User user){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        String query = "select * from user";
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();
        values.put(COLUMN_ID, count);
        values.put(COLUMN_USERNAME, user.getUsername());
        values.put(COLUMN_EMAIL, user.getEmail());
        values.put(COLUMN_PASS, user.getPassword());

        db.insert(TABLE_USER, null, values);
        db.close();
    }
    public String checkUser(String username){
        db = this.getReadableDatabase();
        String query = "SELECT username, password from " + TABLE_USER;
        Cursor cursor = db.rawQuery(query, null);

        String a, b;
        b = "error";
        if(cursor.moveToFirst()){
            do{
                a = cursor.getString(0);


                if(a.equals(username)){
                    b = cursor.getString(1);
                    break;
                }
            }while(cursor.moveToNext());
        }

        return b;

    }


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
chris123
  • 15
  • 6

1 Answers1

0

It seems like you have initially run the application on simulator/device and then add user_table to the onCreate(). In this case, you haven't update the database version.
Either uninstall the app on your simulator/device and then run the project (which will reinstall the app). Or alternatively you can just update the database version.

//i.e
private static final int DATABASE_VERSION = 2;
isamirkhaan1
  • 749
  • 7
  • 19