'Print SQL values in Android studio Java

I am trying to make a function that when you open the app the first thing it does it checks with a query if there are any null values in SQL the code is this:

public static String checkParameters(Context ctx) {        
    Parameters prm = new Parameters(ctx);
    SQLiteDatabase db = prm.getWritableDatabase();
    String count = "select * from table paramname where paramvalue=''";
    Cursor c = db.rawQuery(count, null);
    c.moveToFirst();
    int icount = c.getInt(0);
    if(icount > 0){
        "Success";
    }else{
        "Error";
    }
    return null;
}

But this isn't working I need to make the function check which paramname has paramvalue empty and print that paramname.



Solution 1:[1]

I sense that your bigger problem is on the SQL side, so here is a query you can use to detect what you want:

SELECT COUNT(CASE WHEN paramname = 'OWNER' AND paramvalue IS NOT NULL
                  THEN 1 END) AS flag;

This will return a single record result set, with the value being 1 when OWNER has a non null value. Note that I am assuming that an empty parameter value is represented by NULL, and not empty string. It should be represented by NULL.

Sample Android code:

String sql = "SELECT COUNT(CASE WHEN paramname = 'OWNER' AND ";
sql += "                             paramvalue IS NOT NULL " +
sql += "                        THEN 1 END) AS flag";
Cursor c = db.rawQuery(sql, null);
int cnt = 0;
if (c.moveToFirst()) {
    cnt = c.getInt(0);
}

if (cnt > 0) {
    // success
}
else {
    // failure
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1