-2

i have got the error . login activity when i run the app if i enter the username and password and click login button suddenly app has closed and displayed the message, unfortunately, app has stoped i show the error at logcat i attched below and i attached code belew what i tried so far.

     android.database.sqlite.SQLiteException: no such table:
 user (code 1): , while compiling:
 SELECT id,user,pass FROM user WHERE user=? AND pass=?
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

login

 ed1 = findViewById(R.id.user);
        ed2 = findViewById(R.id.pass);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);



 public  void  login()
    {
        String user = ed1.getText().toString();
        String pass = ed2.getText().toString();
        if (user.equals("") || pass.equals("")) {
            Toast.makeText(this, "Username or Password blank", Toast.LENGTH_LONG).show();
        }
        else if (null!=checkUser(user,pass))
        {
            String userFromDb=checkUser(user,pass);

            Intent i = new Intent(login.this, MainActivity.class);
            i.putExtra("uname", userFromDb);
            startActivity(i);
        }
        else {
            Toast.makeText(this, "Username or Password not match", Toast.LENGTH_LONG).show();
            ed1.setText("");
            ed2.setText("");
            ed1.requestFocus();
        }
    }

    public String checkUser(String name, String pass)
    {
        SQLiteDatabase db = openOrCreateDatabase("pos", Context.MODE_PRIVATE, null);
        Cursor cursor=db.rawQuery("SELECT id,user,pass FROM user WHERE user=? AND pass=?",new String[]{name,pass});
        if(cursor.getCount()>0) {
            cursor.moveToFirst();
            String username = cursor.getString(1);
            String password = cursor.getString(2);
            SharedPreferences.Editor sp = getSharedPreferences("username", MODE_PRIVATE).edit();
            sp.putString("uname", username);
            sp.apply();
            cursor.close();
            return username;
        }
        return null;
    }}

login xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User"
        />

    <EditText
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:textAlignment="center" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"
        />

    <EditText
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:password="true"
        android:textAlignment="center" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">


    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:text="LogIn" />
paypal kob
  • 33
  • 6
  • What is your table name `user` or `users` ? The error tells you have written something wrong. – Xenolion Apr 26 '19 at 05:38
  • it says your table `user` doesn't have a column `user` – Vladyslav Matviienko Apr 26 '19 at 05:43
  • user table name i have created – paypal kob Apr 26 '19 at 05:44
  • insert into user(user,pass,conpass) here sir it has user colum it worked yesterday today i got the problem on login – paypal kob Apr 26 '19 at 05:44
  • ah sorry, it says that there is no such table `user`. Probably you are connecting to wrong DB, or it is empty – Vladyslav Matviienko Apr 26 '19 at 05:47
  • sir i just got the problem yesterday my sqlite database worked properly today itself it didn't worked how to correct please tell me thats way this error was displayed – paypal kob Apr 26 '19 at 05:50
  • If you're certain that your database should have a table named `user`, then try uninstalling/reinstalling your app. If that doesn't work, then [edit] your question to provide a [mcve] that demonstrates the issue, including the `CREATE TABLE` statement, and any other code that calls `openOrCreateDatabase()`. – Mike M. Apr 26 '19 at 05:56
  • Possible duplicate of [Caused by: android.database.sqlite.SQLiteException: no such table: (code 1) Android](https://stackoverflow.com/questions/24634116/caused-by-android-database-sqlite-sqliteexception-no-such-table-code-1-andr) – Arbaz Pirwani Apr 26 '19 at 05:58

2 Answers2

0

Have you checked with the table name? suggestion: Create a string with the Table name in DatabaseHelper class and use it.

Also, you may need readable access for the database before firing the SQLite query.

SQLiteDatabase db = this.getReadableDatabase();

Please find the sample code for checking user at the time of logging in.

public boolean checkUser(String email, String password) {

    String[] columns = {COLUMN_USER_ID};
    SQLiteDatabase db = this.getReadableDatabase();

    String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";

    String[] selectionArgs = {email, password};

    Cursor cursor = db.query(TABLE_USER, //Table to query
            columns,                    //columns to return
            selection,                  //columns for the WHERE clause
            selectionArgs,              //The values for the WHERE clause
            null,                       //group the rows
            null,                       //filter by row groups
            null);                      //The sort order

    int cursorCount = cursor.getCount();

    cursor.close();
    db.close();
    if (cursorCount > 0) {
        return true;
    }

    return false;
}
Pratik Bhandari
  • 248
  • 1
  • 13
0

Try to remove your app from your emulator or from your phone and run again, i've got this same error, and reinstalling worked for me