-2

*

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 ... *

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amin
  • 342
  • 4
  • 15
  • what exactly are you doing or trying to achieve? numerical passwords instead of strings? – Hudi Ilfeld Nov 05 '18 at 15:26
  • It's not clear how the SQL SUM() function has a role in that. – Phantômaxx Nov 05 '18 at 16:23
  • My problem is that I enter the information using the function INSERT and check the information with the function LOGIN for the entry operation but l dont know for why, when i using password of number type(for example 1996) , function LOGIN ,give me return is false . however and when i using password string type (for example root) function LOGIN ,give return is true. summry , i can NOT using password or username type of number . – Amin Nov 05 '18 at 18:27
  • i can NOT using password or username, type of number . – Amin Nov 05 '18 at 18:34
  • Given that all passwords should be **strings** (alphanumeric, alphabetic in mixed case and numeric characters all in one password), why in the world would you ever want to **SUM()** your passwords? – Phantômaxx Nov 07 '18 at 08:37

1 Answers1

0

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;
MikeT
  • 51,415
  • 16
  • 49
  • 68