Search code examples
javaandroidsqliteandroid-sqlitesqliteopenhelper

How can I use the SQLite SUM() function?


*

My problem

Recently, I ran some of my Java code [Open Helper] through android studio , and it gave the following error : I have an login page with android (java code) and just my problem when time that must be password just string put , will be true. but if was put numerical password , will not true . code SqlOpenHelper

package com.example.root.sql2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class db extends SQLiteOpenHelper {
    public db(Context context) {
        super(context, "login.db", null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE USER(ID INTEGER PRIMARY KEY AUTOINCREMENT ,NAME TEXT, PASSWORD TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS USER");
        onCreate(sqLiteDatabase);
    }
    public boolean insert (String name , String password){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("NAME", name);
        contentValues.put("PASSWORD", password);
        long ins = db.insert("USER","",contentValues);
        if (ins == -1) return false;
        else return true;
    }
    public boolean login(String name , String password){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM USER WHERE NAME=? AND PASSWORD=?", new String[] {name , password});
        cursor.moveToFirst();
        if (cursor.getCount()>0) return false;
        else return true;
    }
}

"password" if was string ,password then is true but "password" if was string ,password(numerical) then will not true ... *


Solution

  • I believe your main issue is that your logic is reversed.

    You are basically saying by using if (cursor.getCount()>0) return false;; if the search for the user found the user then return false.

    I believe you want if (cursor.getCount()>0) return true;

    However, albeit it not an issue, the use of moveToFirst adds nothing of use. Additionally you may encounter issues because you are not closing the cursor.

    I'd suggest that you perhaps use :-

    public boolean login(String name , String password){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM USER WHERE NAME=? AND PASSWORD=?", new String[] {name , password});
        int count = cursor.getCount();
        cursor.close();
        return count > 0;
    }
    
    • This gets the count, closes the cursor, then returns true if the count is greater than 0 (user password combination was found) otherwise false;