• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps A cursor question

Gumtrees

Lurker
Mar 27, 2017
4
0
Hi guys, sorry for registering and then immediately asking a question, but I'm at a loss. I'm trying to make a register/login function, with a local database (I know it's pointless to make a local login database function, but I'm a beginner and just want to make it work to prove to myself, before I make an actual working one)

I have a database helper that looks like ...

Code:
public class DatabaseHelperClass extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "User.db";
    public static final String TABLE_NAME = "user_Info";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "EMAIL";
    public static final String COL_4 = "PASSWORD";

    public DatabaseHelperClass(Context context) {  //Constructor
        super(context, DATABASE_NAME, null, 1);

    }

    [USER=1021285]@override[/USER]
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,EMAIL TEXT,PASSWORD TEXT)");
        //Auto incrementing PRIMARY KEY so it remains unique

    }

    [USER=1021285]@override[/USER]
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }//On edit


    public boolean insertData(String name, String email, String password) {

        SQLiteDatabase db = this.getWritableDatabase();    //Putting data into database
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, email);
        contentValues.put(COL_4, password);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }
}

Now I have a 'Register Page' which the user can add their data to the database, that works fine. Now I just need to be able to read the database, and match a user's email and password with an entry within the database. Can anyone point me in the right direction? Or to any useful tutorials, because most of the ones I've came across aren't really that useful. I know I need to use cursors, but I just need a starting point really. Any help appreciated.

Thanks.
 
Last edited by a moderator:
You can do this with a simple query method in your DatabaseHelperClass.
The following checks that the cursor contains exactly one record. Watch out for the " and ' characters.

Code:
public boolean passwordOk(String email, String password) {
     SQLiteDatabase db = this.getReadableDatabase();
     Cursor res =  db.rawQuery( "select * from " +  TABLE_NAME + " where " + COL_3 + "='"+ email + '" and " + COL_4 + "='" + password + "'", null );
 
     return (res.getCount() == 1);
   }
 
Last edited by a moderator:
Upvote 0
Thanks for the response! Just had chance to look at it. Within my MainActivity class, I've put the following code

Code:
public void passwordOK(){

    loginB.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v) {
               boolean isInsert = myD.passwordOk(editEmail.getText().toString(), editPassword.getText().toString());
                    if (isInsert ==true)
                        Toast.makeText(MainActivity.this, "You Have Successfully Logged in", Toast.LENGTH_LONG).show();
             else  Toast.makeText(MainActivity.this, "You Have Not Logged In", Toast.LENGTH_LONG).show();



                }
            }
    );
}


With this in my DatabaseHelper

Code:
public boolean passwordOk(String email, String password) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from " +  TABLE_NAME + " where " + COL_3
            + "='"+ email +  COL_4 + "='" + password + "'", null );

    return (res.getCount() == 1);
}


But it isn't working. Any pointers? I know I'm probably missing something obvious, but I'm such a rookie to Android I'm not sure what.

thanks for any more help.
 
Last edited by a moderator:
Upvote 0
It's closing my application. I just want it to iterate through the database, and find a matching email/password

Here's the log;

03-29 21:15:08.929 4402-4402/com.example.onein.jobgosite I/art: Not late-enabling -Xcheck:jni (already on)
03-29 21:15:08.946 4402-4407/com.example.onein.jobgosite E/art: Failed sending reply to debugger: Broken pipe
03-29 21:15:08.946 4402-4407/com.example.onein.jobgosite I/art: Debugger is no longer active
03-29 21:15:08.961 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.042 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.056 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.067 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.079 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.089 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.101 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.111 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.120 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.131 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.142 4402-4402/com.example.onein.jobgosite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.onein.jobgosite-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.onein.jobgosite-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
03-29 21:15:09.142 4402-4402/com.example.onein.jobgosite W/System: ClassLoader referenced unknown path: /data/app/com.example.onein.jobgosite-1/lib/x86
03-29 21:15:09.143 4402-4402/com.example.onein.jobgosite I/InstantRun: Starting Instant Run Server for com.example.onein.jobgosite


EDIT - Sorry for the messy log
 
Upvote 0
Woops, sorry. Here it is

03-30 12:10:54.949 2605-2605/com.example.onein.jobgosite E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.onein.jobgosite, PID: 2605
android.database.sqlite.SQLiteException: near "doh": syntax error (code 1): , while compiling: select * from user_Info where EMAIL='doPASSWORD='doh'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.onein.jobgosite.DatabaseHelperClass.passwordOk(DatabaseHelperClass.java:62)
at com.example.onein.jobgosite.MainActivity$2.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones