When I try to run my application, it crashes again and again. below is the method that I have created in DBHandler.java file
public Cursor CheckUsername() {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM register WHERE Username = " MainActivity.getValue() " and password = " MainActivity.getpassword() ";";
return db.rawQuery(query,null);
}
So to check whether username and password is correct I have also created checklogin()
method in MainActivity.java
below is the code
private void checkLogin() {
final EditText editText = findViewById(R.id.emaillogin);
final EditText editText1 = findViewById(R.id.passwordlogin);
//editText.setText("");
password = editText1.getText().toString();
//editText1.setText("");
username = editText.getText().toString().trim();
Cursor c = dbHandler.CheckUsername();
if (c.moveToFirst()) {
do {
Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
while (c.moveToNext());
}
}
When I try to debug it shows
E/SQLiteLog: (1) no such column: rushi
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.software2.bloodbankmanagement, PID: 27590
android.database.sqlite.SQLiteException: no such column: rushi (code 1): , while compiling: SELECT * FROM register WHERE Username = rushi and password = 4567;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
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:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1404)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1336)
at com.example.software2.bloodbankmanagement.DBHandler.CheckUsername(DBHandler.java:176)
at com.example.software2.bloodbankmanagement.MainActivity.checkLogin(MainActivity.java:94)
at com.example.software2.bloodbankmanagement.MainActivity.access$000(MainActivity.java:31)
at com.example.software2.bloodbankmanagement.MainActivity$1.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:6412)
at android.view.View$PerformClick.run(View.java:25341)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6977)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
CodePudding user response:
check out with this query
String query = "SELECT * FROM register WHERE Username = '" MainActivity.getValue() "' and password = '" MainActivity.getpassword() "';";
note '
character before and after every String
. this should produce below query
SELECT * FROM register WHERE Username = 'value' and password = 'password';
btw. passing data with static reference kept in Activity
(e.g. MainActivity.getValue()
) is a VERY bad aproach, it leads you to future memory leaks. you should pass these credentials to checkLogin
method. also handling database in main thread is very bad for performance, when your database grow then searching in it would be taking some time, during it you apps UI will be freezed. strongly consider threading, coroutines, just any async way for processing data