[Java for Android App Development] ariftwister:
Hi Guys,I am Developing an Android App for Local Bus Schedule, When Applying a specific Query, My App Crashes in As Soon as it starts.

Here is MY LOG:

03-12 07:33:54.564: E/SQLiteLog(3580): (1) near "pnotocdm": syntax error
03-12 07:33:54.564: D/AndroidRuntime(3580): Shutting down VM
03-12 07:33:54.564: W/dalvikvm(3580): threadid=1: thread exiting with uncaught exception (group=0x41665c80)
03-12 07:33:54.574: E/AndroidRuntime(3580): FATAL EXCEPTION: main
03-12 07:33:54.574: E/AndroidRuntime(3580): Process: com.arif.pnobusfinder, PID: 3580
03-12 07:33:54.574: E/AndroidRuntime(3580): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arif.pnobusfinder/com.arif.pnobusfinder.MainActivity}: android.database.sqlite.SQLiteException: near "pnotocdm": syntax error (code 1): , while compiling: SELECT * pnotocdm where KEY_TIME = ?
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.ActivityThread.access$800(ActivityThread.java:145)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.os.Handler.dispatchMessage(Handler.java:102)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.os.Looper.loop(Looper.java:136)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.ActivityThread.main(ActivityThread.java:5081)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at java.lang.reflect.Method.invokeNative(Native Method)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at java.lang.reflect.Method.invoke(Method.java:515)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at dalvik.system.NativeStart.main(Native Method)
03-12 07:33:54.574: E/AndroidRuntime(3580): Caused by: android.database.sqlite.SQLiteException: near "pnotocdm": syntax error (code 1): , while compiling: SELECT * pnotocdm where KEY_TIME = ?
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at com.arif.pnobusfinder.DatabaseHandler.getAllBus(DatabaseHandler.java:103)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at com.arif.pnobusfinder.MainActivity.onCreate(MainActivity.java:55)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.Activity.performCreate(Activity.java:5231)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-12 07:33:54.574: E/AndroidRuntime(3580):	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
03-12 07:33:54.574: E/AndroidRuntime(3580):	... 11 more

Here's my Code.

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "timetable";

// Bus table name
static final String TABLE_PNOTOCDM = "pnotocdm";

// Bus Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_BUSNAME = "busname";
private static final String KEY_TIME = "time";

...
public List<Pnotocdm> getAllBus() {
List<Pnotocdm> busList = new ArrayList<Pnotocdm>();
// Select All Query
String selectQuery = " SELECT * " + TABLE_PNOTOCDM + " where KEY_TIME = ? " ;

SQLiteDatabase db = this.getWritableDatabase();
Calendar cal = Calendar.getInstance();
int minute = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR);

String time = String.valueOf(hour)+String.valueOf(minute);
Cursor cursor = db.rawQuery(selectQuery, new String[] {time});

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Pnotocdm bus = new Pnotocdm();
bus.setID(Integer.parseInt(cursor.getString(0)));
bus.setBusName(cursor.getString(1));
bus.setTime(cursor.getString(2));

String name = cursor.getString(1) +"\n"+ cursor.getString(2);
Third.ArrayofName.add(name);
// Adding bus to list
busList.add(bus);
} while (cursor.moveToNext());
}

// return bus list
return busList;
}

I Know the rawquery is wrong but i dont know the Correct Query.

What I Intended is this, The System should compute the Current Time and Check with time Column in the table and Return those values which are greater than the current Time.

The Correct Query Which works in sqlite Browser is

SELECT busname,time FROM pnotocdm WHERE time>time('now','localtime');

But I dont know how to convert that sql query into java rawquery() method. Please Help