简体   繁体   中英

Sql Query to retrieve a particular data from particular column and row in android?

I want to fetch phone number linked to particular email in the database. I am not able to find the query for it or how

public String getContactNumber(String email){
            SQLiteDatabase db = this.getReadableDatabase();
            String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER + " FROM " + TABLE_USER + " WHERE " + email +  " = " + COLUMN_USER_EMAIL;
            Cursor cursor = db.rawQuery(query,null);
            //What to put here to extract the data.

            String contact = cursor.getString(get); 
            cursor.close();
            return contact;
        }

to extract the data. Completely a beginner

Try this ..

public List<String> getMyItemsD(String emailData) {
    List<String> stringList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT COLUMN_USER_MOBILE_NUMBER FROM " + USER_TABLE_NAME + " WHERE email= " + emailData;
    Cursor c = db.rawQuery(selectQuery, null);
    if (c != null) {
        c.moveToFirst();
        while (c.isAfterLast() == false) {
            String name = (c.getString(c.getColumnIndex("Item_Name")));

            stringList.add(name);
            c.moveToNext();
        }
    }
    return stringList;
}
public String getContactNumber(String email){
    String contact = "";
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER + " FROM " + TABLE_USER + " WHERE " + email +  " = " + COLUMN_USER_EMAIL;
    Cursor cursor = db.rawQuery(query,null);

    if(cursor.getCount()>0) {
        cursor.moveToNext();
        contact = cursor.getString(cursor.getColumnIndex(COLUMN_USER_MOBILE_NUMBER)); 
    }
    //What to put here to extract the data.                             
    cursor.close();
    return contact;
}

From this method you get phone number value of that email which you pass any other method easily.

I'd suggest the following :-

public String getContactNumber(String email){
    String contact = "NO CONTACT FOUND"; //<<<<<<<<<< Default in case no row is found.
    SQLiteDatabase db = this.getWritableDatabase(); //<<<<<<<<<< Generally getReadable gets a writable database 
    String[] columns_to_get = new String[]{COLUMN_USER_MOBILE_NUMBER};
    String whereclause = COLUMN_USER_EMAIL + "=?";
    String[] whereargs = new String[]{email};
    Cursor cursor = db.query(TABLE_USER,columns_to_get,whereclause,whereargs,null,null,null);
    //What to put here to extract the data.
    if (cursor.moveToFirst()) {
        contact = csr.getString(csr.getColumnIndex(COLUMN_USER_MOBILE_NUMBER));
    }
    cursor.close();
    return contact;
}
  • The above does assumes that there will only be 1 row per email (which is most likely).

Explanations

A default value is set so that you can easily tell if an invalid/non-existent email is passed (you'd check the return value if need be (might be easier to simply have "" and check the length as a check)).

getReadableDatabase has been replaced with getWritableDatabase as unless there are issues with the database a writable database will be returned, as per :-

Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future. getReadableDatabase

  • Note no real problem either way;

The recommended query method has been used instead of the rawQuery method. This has distinct advantages, it builds the underlying SQL and also offers protection against SQL injection (just in case the email passed is input by a user).

  • this version of the method takes 7 parameters :-
    • The table name as a string
    • The columns to be extracted as an array of Strings (aka String array). null can be all columns.
    • The where clause less the WHERE keyword with ?'s to represent arguments (see next). null if no WHERE clause.
    • The arguments to be applied (replace ?'s 1 for 1) as a String array. null if none or no WHERE clause.
    • The GROUP BY clause, less the GROUP BY keywords. null if no GROUP BY clause.
    • The HAVING clause, less the HAVING keyword. null if no HAVING clause.
    • The ORDER BY clause, less the ORDER BY keywords. null if no ORDER BY clause.

SQLiteDatabase - query - Note there are 4 query methods (see link for the subtle difference, I believe this is the most commonly used)

The data extraction is the new code. When a Cursor is returned it is at a position BEFORE THE FIRST ROW, so you need to move to a valid row. So the moveToFirst* method is suitable (note that if a move cannot be made by a move method that it will return false, hence how you can say if (cursor.moveToFirst()) ) . The data is then extracted from the appropriate column use the **getString method, which takes an int as an argumnet for the column offset (0 in this case). However, using hard coded values can lead to issues so the getColumnIndex method is used to get the offset according to the column name (-1 is returned if the named column is not in the Cursor).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM