lundi 2 mai 2016

RawQuery doesn't work when given null valueto args

I'm writing a basic android app in which personal information like name, email, etc. is filled in and stored in an SQLite database by pressing a "submit"-button (this all works perfectly). Then there's a search button which should search for a corresponding person in the database when one field is filled in (e.g. only the field name is given a string "harry" and the other fields keep a null-value). This is the corresponding code in my DatabaseHelper class:

public Person getPerson(String naam, String adres, String email, String geslacht,
        String leeftijd, String geboortedatum){
    Log.i("Main activity","Person to find: "+naam);
    SQLiteDatabase db= this.getReadableDatabase();
    Cursor curs=db.rawQuery("SELECT name FROM personen WHERE (name = ?) OR (adress = ?) "
            + "OR (email = ?) OR (gender = ?) OR (age = ?) OR (birthday = ?)", 
            new String[]{naam, adres, email, geslacht, leeftijd, geboortedatum});
    Log.i("Main activity", "Query works");

I used the logs to make sure where the error is thrown and found that the rawQuery does work when all the args[] fields are given a value, but gives an error when one field (e.g. email) contains a null value. Isn't this why I should use the OR statement instead of the AND statement? If not, how can I make it work when not all fields are filled in in the app?

Aucun commentaire:

Enregistrer un commentaire