mardi 15 septembre 2015

SQLite query in Android not working

I am trying to write a query, but my aplication crashes and I don't see the reason, I have function like this

public Cursor getPraktikum(long sat, String dan) throws SQLException {
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[]{COLUMN_NAME_NAME}, COLUMN_NAME_DAY + "=" + dan + "AND" + "(" + COLUMN_NAME_START + ">=" + sat + "AND" + COLUMN_NAME_END + "<" + sat + ")", null,
                    null, null, null, null); 
      // Cursor mCursor=db.rawQuery("SELECT" + COLUMN_NAME_NAME + "FROM " + DATABASE_TABLE + "WHERE" + COLUMN_NAME_DAY + "=" +dan + "AND" + "(" + COLUMN_NAME_START + ">=" + sat + "AND" + COLUMN_NAME_END + "<" + sat + ")", null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

As you see I tried both query and rawQuery versions none work. "Sat" and "Dan" are gained like this.

Calendar kalendar = Calendar.getInstance();
    int sat = kalendar.get(Calendar.HOUR_OF_DAY);
    int dani= kalendar.get(Calendar.DAY_OF_WEEK);
    String dan=new String();
    if(dani==3) {
        dan ="Utorak";
    }
    DatabaseHelper dbi=new DatabaseHelper(this.getApplicationContext());
    dbi.open();
  //  Cursor c=dbi.getTermin(1);
   Cursor cu = dbi.getPraktikum(sat,dan);
 /*   if (cu.moveToFirst())
        DisplayContact(cu); */
    dbi.close();
}
public void DisplayContact(Cursor c)
{
    Toast.makeText(this,
            "id: " + c.getString(0) + "\n",

            Toast.LENGTH_LONG).show();
}

I was leading myself with the function whose getTermin(long rowId) which works and goes like this

public Cursor getTermin(long rowId) throws SQLException {
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,
                            COLUMN_NAME_NAME, COLUMN_NAME_DAY, COLUMN_NAME_START, COLUMN_NAME_END}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

I've googled everything but I just can't seem to find why function getPraktikum isnt' working and getTermin does, please help!

Aucun commentaire:

Enregistrer un commentaire