Home > Net >  Query in SQLite not read in Android Studio
Query in SQLite not read in Android Studio

Time:08-17

I've been wondering why my Cursor returns an empty value If I Select the hh_id fields, it works on normal number like 1234 but I observed when the hh_id consists of symbol - it returns the cursor to null even though it exists

enter image description here

String household_no ="";
household_no = edt_hh.getText().toString();

Cursor search = MainActivity.sqLiteHelper.getData("SELECT id,full_name,hh_id,client_status,address,sex,hh_set_group,current_grantee_card_number,other_card_number_1,other_card_holder_name_1,other_card_number_2,other_card_holder_name_2,other_card_number_3,other_card_holder_name_3,upload_history_id,created_at,updated_at,validated_at FROM emv_database_monitoring WHERE hh_id=" household_no);
           
            while (search.moveToNext()) {
                String emv_id = search.getString(0);
                String full_name = search.getString(1);
                String hh_id = search.getString(2);
                String client_status = search.getString(3);
                String address = search.getString(4);
                String sex = search.getString(5);
                String hh_set_group = search.getString(6);
                String current_grantee_card_number = search.getString(7);
                String other_card_number_1 = search.getString(8);
                String other_card_holder_name_1 = search.getString(9);
                String other_card_number_2 = search.getString(10);
                String other_card_holder_name_2 = search.getString(11);
                String other_card_number_3 = search.getString(12);
                String other_cardholder_name_3 = search.getString(13);
                String upload_history_id = search.getString(14);
                String created_at = search.getString(15);
                String updated_at = search.getString(16);
                String validated_at = search.getString(17);
            }
            Log.v(ContentValues.TAG,"hahaha "  hh_id);
            search.close();

CodePudding user response:

Because of WHERE hh_id=" household_no.

When household_no contains dashes i.e. minus signs, it's a math expression that gets evaluated and the result of that expression does not necessarily match any rows.

You should at least use single quotes for literal values, such as WHERE hh_id='" household_no "'", or better yet, use selectionArgs variable binding (example).

  • Related