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" />