Filipe Filardi
Filipe Filardi

Reputation: 170

SQLite Select statement when have multiples rows

I want select * from a table comparing 3 rows and the select return an id, i create this method:

//UserDAO class

private SQLiteDatabase mDatabase;
private DatabaseHelper mDbHelper;

// irelative code

public Long selectUserLogin(String nickname, String chapter, String password) {
    long id;
    String selectQuery = "SELECT  * FROM " +
            DatabaseHelper.TABLE_USERS + " WHERE " +
            DatabaseHelper.COLUMN_USERS_NICKNAME + " = " + nickname + " AND " +
            DatabaseHelper.COLUMN_USERS_CHAPTER + " = " + chapter + " AND " +
            DatabaseHelper.COLUMN_USERS_PASSWORD + " = " + password;

    Log.e(TAG, selectQuery);
    try {
        Cursor cursor = mDatabase.rawQuery(selectQuery, null);
        if (cursor != null)
            cursor.moveToFirst();

        id = cursor.getLong(cursor.getColumnIndex(DatabaseHelper.COLUMN_USERS_ID));

        return id;
    } catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

Already created on my database nickname = q, chapter = Q and password = qqqqqq

These is where i called the method to test his functionality

userDAO.selectUserLogin("q", "Q", "qqqqqq");

I checked with another method that list all my users and i get all the users I've one time created.

Here is the results:

10-09 01:00:18.298 10978-10978/com.org.feedme.cisvmeeting.activities     D/LeaderFragment: User: [
D/LeaderFragment: [1] 1, h, h, G 
D/LeaderFragment: [2] 2, fkfkdk, fjfkdk, SAO 
D/LeaderFragment: [3] 3, Filipe fila, Fil36
D/LeaderFragment: [4] 4, q, Q, qqqqqq
D/LeaderFragment: [5] 5, glgl, vmlc, GOLGO]

When i tried to see my data folder on Android Device Monitor the folder was empty, so i'm little bit confused if i could not access the database or if i'm not inserting correctly.

Part of my logcat when i tried call the method selectUserLogin:

10-09 00:46:01.198 7745-7745/com.org.feedme.cisvmeeting.activities E/UserDAO: SELECT * FROM users WHERE nickname = q AND chapter = Q AND password = qqqqqq

10-09 00:46:01.208 7745-7745/com.org.feedme.cisvmeeting.activities E/SQLiteLog: (1) no such column: q

10-09 00:46:01.208 7745-7745/com.org.feedme.cisvmeeting.activities W/System.err: android.database.sqlite.SQLiteException: no such column: q (code 1): , while compiling: SELECT * FROM users WHERE nickname = q AND chapter = Q AND password = qqqqqq

EDIT: I only run my app on my android device!

EDIT_2: I tested another similar method and these second one is working fine!

public String selectUser(long id) {
    String selectQuery = "SELECT  * FROM " + DatabaseHelper.TABLE_USERS +
            " WHERE " + DatabaseHelper.COLUMN_USERS_ID + "=" + id ;
    String result = null;
    Log.e(TAG, selectQuery);
    Cursor cursor = mDatabase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        result = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_USERS_NAME));
    }
    return result;
}

Upvotes: 0

Views: 108

Answers (2)

Omey
Omey

Reputation: 11

first of all you remove you application on your device and then reinstall the application now insert the data you needed and now whatever you entered data is remember and apply those data on your query parameter for select.

Upvotes: 0

jyanks
jyanks

Reputation: 2396

You need to surround the string arguments in quotes, since you are not using selectionArgs (the second parameter in rawQuery).

String selectQuery = "SELECT  * FROM " +
        DatabaseHelper.TABLE_USERS + " WHERE " +
        DatabaseHelper.COLUMN_USERS_NICKNAME + " = '" + nickname + "' AND " +
        DatabaseHelper.COLUMN_USERS_CHAPTER + " = '" + chapter + "' AND " +
        DatabaseHelper.COLUMN_USERS_PASSWORD + " = '" + password + "' ";

Log.e(TAG, selectQuery);
try {
    Cursor cursor = mDatabase.rawQuery(selectQuery, null);

Or you can do this, which will take care of the strings for you.

String selectQuery = "SELECT  * FROM " +
        DatabaseHelper.TABLE_USERS + " WHERE " +
        DatabaseHelper.COLUMN_USERS_NICKNAME + "=? AND " +
        DatabaseHelper.COLUMN_USERS_CHAPTER + "=? AND " +
        DatabaseHelper.COLUMN_USERS_PASSWORD + "=? ";

String[] selectionArgs = new String[] { nickname, chapter, password };

Log.e(TAG, selectQuery);
try {
    Cursor cursor = mDatabase.rawQuery(selectQuery, selectionArgs);

Upvotes: 2

Related Questions