Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] How to retrieve the result of a select query in Sqlite java android?

Posted on November 11, 2022 By

Solution 1 :

Use rawQuery() method to get a Cursor on which you don’t have to iterate.
Just check if 1 row is returned because this is what you want to know: if a user exists with the specific login and password:

public boolean checkAuthentication(String login, String password){
    boolean check = false;
    String sql = "SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?";
    SQLiteDatabase db = dbManager.getWritableDatabase();
    Cursor cursor = db.rawQuery(sql, new String[] {login, password});
    check = cursor.moveToFirst();
    cursor.close();
    db.close();
    return check;
}

Problem :

I’m looking to retrieve the result of my request in variables to be able to check if the authentication is correct. In simple sql it does this with a Resultset but I understood that in Sqlite you have to use a Cursor. But I can’t get a result. Here are two examples of what I have already tried :

    public boolean checkAuthentication(String login, String password){

        boolean check = false;

        SQLiteDatabase db = dbManager.getWritableDatabase();
        SQLiteStatement statement = db.compileStatement("SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?");
        statement.bindString(1, login);
        statement.bindString(2, password);
        //statement.execute();
        return check;

    }

    public boolean checkAuthentication2(String login, String password){

        boolean check = false;
        String log = null;
        String pass = null;

        String sql = "SELECT * from Visiteur WHERE vis_login = ? and vis_mdp = ?";
        SQLiteStatement statement = db.compileStatement(sql);
        statement.bindString(1, login);
        statement.bindString(2, password);

        Cursor cursor = db.rawQuery(sql, null);

        while(cursor.moveToNext()){
            log =  cursor.getString(cursor.getColumnIndex("vis_login"));
            pass = cursor.getString(cursor.getColumnIndex("vis_mdp"));
            Log.d("WHILE", log);
        }
        cursor.close();
        statement.close();

        if(log != null && pass != null){
            check = true;
        }


        return check;

    }

Thanks for responses.

Comments

Comment posted by ChadNC

You still need a ResultSet to iterate through using .next().

Comment posted by RMrenex

But I cannot use ResulSet in Sqlite.

Comment posted by RMrenex

But if I needed to store the values ​​to reuse them I proceed as in my second example?

READ  [FIXED] xamarin.android - send sms to whatsapp xamarin android
Powered by Inline Related Posts

Comment posted by forpas

If you mean the values of login and password you already have them because you pass them to the method.

Comment posted by RMrenex

Sorry I misspoke. If I wanted to make a

Comment posted by forpas

Then you need another cursor:

Android Tags:android, android-sqlite, java, sqlite

Post navigation

Previous Post: [FIXED] java – The Android LoaderManager is deprecated. Now what?
Next Post: [FIXED] android – Is WorkManager the right solution for me?

Related Posts

[FIXED] android – ERROR: Gradle DSL method not found: ‘implementation()’ Android
[FIXED] java – How can I share array lists between activities in android studio? Android
[FIXED] android – BroadcastReceiver not working for PendingIntent(…) Android
[FIXED] Asynctask to open a web page in background Android Kotlin Android
[FIXED] Android ViewModel architecture for inter fragment communication Android
[FIXED] google cloud storage – AndroidX Camera Core ImageAnalysis.Analyser results in distorted image Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme