javaandroidsqlitesmd

Android application fetch data from SQLite


I am new to android programming and i am trying to fetch data from SQLite and display it in list View. This is where i am getting and displaying data

DataHandler handler=new DataHandler (getBaseContext());
handler.open();
Cursor c=handler.returnData_schedule();
List<ScheduleData> arr=new ArrayList<ScheduleData>();
if(c.moveToFirst()){
    do{
        String date=c.getString(0);
        String time=c.getString(1);
        String food=c.getString(2);
        ScheduleData myData=new ScheduleData(date,time,food);
        arr.add(myData);
    }while(c.moveToNext());
}           
ScheduleAdapter myadapter=new ScheduleAdapter(this,R.layout.schedule_list,arr);
ListView listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(myadapter);

this is my fetch function.

public Cursor returnData_schedule()
    {
        return db.query(TABLE_SCHEDULE,new String[] {date,time,food},null,null,null,null,null);
    }

This is my adapter class

   public class ScheduleAdapter extends ArrayAdapter<ScheduleData> {

    Context myContext;
    int layout;
    List<ScheduleData> obj;
    public ScheduleAdapter(Context context, int resource,List<ScheduleData> arr) {
        super(context, resource, arr);
        myContext=context;
        layout=resource;
        obj=arr;
    }
    @Override
    public View getView(int position, View ConvertView,ViewGroup parent){
        View row;
        if(ConvertView==null){
            LayoutInflater inflater=((Activity) myContext).getLayoutInflater();
            row=inflater.inflate(layout,parent,false);
        }
        else{
            row=(View)ConvertView;
        }
        TextView dateText=(TextView)row.findViewById(R.id.textView1);
        TextView timeText=(TextView)row.findViewById(R.id.textView2);
        TextView foodText=(TextView)row.findViewById(R.id.textView3);

        dateText.setText(obj.get(position).getDate());
        timeText.setText(obj.get(position).getTime());
        foodText.setText(obj.get(position).getFood());      
        return row;
    }

}

This is the logcat

03-25 16:43:38.330: D/dalvikvm(1789): GC_FOR_ALLOC freed 93K, 6% free 3118K/3288K, paused 267ms, total 280ms
03-25 16:43:38.450: I/dalvikvm-heap(1789): Grow heap (frag case) to 3.723MB for 635812-byte allocation
03-25 16:43:38.620: D/dalvikvm(1789): GC_FOR_ALLOC freed 9K, 5% free 3729K/3912K, paused 167ms, total 167ms
03-25 16:43:39.420: E/SQLiteLog(1789): (1) near "FROM": syntax error
03-25 16:43:39.450: D/AndroidRuntime(1789): Shutting down VM
03-25 16:43:39.450: W/dalvikvm(1789): threadid=1: thread exiting with uncaught exception (group=0xb3aa6ba8)
03-25 16:43:39.620: E/AndroidRuntime(1789): FATAL EXCEPTION: main
03-25 16:43:39.620: E/AndroidRuntime(1789): Process: com.example.slimart, PID: 1789
03-25 16:43:39.620: E/AndroidRuntime(1789): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slimart/com.example.slimart.seeschedule}: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT 1, 12,  FROM schedule
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.os.Looper.loop(Looper.java:136)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at java.lang.reflect.Method.invoke(Method.java:515)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at dalvik.system.NativeStart.main(Native Method)
03-25 16:43:39.620: E/AndroidRuntime(1789): Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT 1, 12,  FROM schedule
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at com.example.slimart.DataHandler.returnData_schedule(DataHandler.java:103)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at com.example.slimart.seeschedule.onCreate(seeschedule.java:20)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.Activity.performCreate(Activity.java:5231)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 16:43:39.620: E/AndroidRuntime(1789):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-25 16:43:39.620: E/AndroidRuntime(1789):     ... 11 more
03-25 16:43:43.720: I/Process(1789): Sending signal. PID: 1789 SIG: 9
03-25 16:43:46.350: D/gralloc_goldfish(1818): Emulator without GPU emulation detected.

I have no idea why it crashes. I have used the same method to fetch data from other table and it gives me correct data. I do not know know what syntax error it is talking about in 03-25 16:43:39.420: E/SQLiteLog(1789): (1) near "FROM": syntax error Please guys tell me how to solve this issue. Thanks in advance.


Solution

  • return db.query(TABLE_SCHEDULE,new String[] {date,time,food},null,null,null,null,null);
    

    In here data,time,food ought to be column names and not values. You get the syntax error when food is an empty string.

    I don't know your table schema, but guessing they are the column names, it should read:

    return db.query(TABLE_SCHEDULE,new String[] {"date","time","food"},null,null,null,null,null);
    

    with the column names quoted as strings.