0

I am getting error in my sql Query and what are things i need to change to improve my coding

My Code is

My DBHelper Code

public Cursor fetchDaysAppointment(String from, String to){

    Cursor c = ourDatabase.rawQuery("SELECT * FROM "+CUSTOMER_TABLE_NAME+
            "WHERE (aDate >= ? AND aDate <= ?)", 
            new String[]{from, to});

    if(c != null){
        c.moveToFirst();
    }
    return c;
}

My Java Code

public void getDaysAppointment(int day, int month, int year){

    String from = year+"-"+month+"-"+day+" 00:00:00";
    String to = year+"-"+month+"-"+day+" 23:59:59";

    in.open();
    cursor = in.fetchDaysAppointment(from, to);
    cursor.moveToFirst();

    for(int i=0; i<cursor.getCount(); i++){

        if(cursor.getInt(cursor.getColumnIndex("aTime")) == 8){
            System.out.println("Hi the value is found to be Matched");
        }
        cursor.moveToNext();

    }

}

Log cat Error

02-14 17:23:06.740: E/AndroidRuntime(4535): FATAL EXCEPTION: main
02-14 17:23:06.740: E/AndroidRuntime(4535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ashadegreen/com.example.ashadegreen.Home}: android.database.sqlite.SQLiteException: near "(": syntax error: , while compiling: SELECT * FROM customerWHERE (aDate >= ? AND aDate <= ?)
user2065795
  • 741
  • 1
  • 6
  • 8

2 Answers2

2

You forgot to add space before WHERE.

uthark
  • 5,333
  • 2
  • 43
  • 59
  • hey you r right....do you have any idea why this is not working...the value 8 exist in my tableif(cursor.getInt(cursor.getColumnIndex("aTime")) == 8){ System.out.println("Hi the value is found to be Matched"); } – user2065795 Feb 14 '13 at 17:51
  • if condition i am checking for value 8....why it is not working? – user2065795 Feb 14 '13 at 17:51
  • @user2065795 According to your aTime is a date, how can you call getInt on a column that have date? I guess, you should call cursor.getInt(cursor.getColumnIndex("XXX")) where XXX is the name of the column which contains your value 8. – uthark Feb 14 '13 at 17:57
2

What are things i need to change to improve my coding.?

One thing i see is that you are defining CUSTOMER_TABLE_NAME and others in your DBHelper class itself, for small programs that's fine, but when your code grows and the Database Schema becomes bigger, it is important to maintain code and db.

For that, make another class lets say named Constants, and define all your Database schema related information, like dbname, table's, their columns in that same class, and later you can access them using like Constants."itemyouwanttouse" . This will make your code a lot more organized.

jaymeht
  • 678
  • 1
  • 8
  • 14