2014-03-25 57 views
1

我是新來的android編程,我試圖從SQLite中獲取數據並將其顯示在列表視圖中。 這是我得到並顯示數據的地方Android應用程序從SQLite獲取數據

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); 

這是我的獲取函數。

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

這是我的適配器類

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; 
    } 

} 

這是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. 

我不知道爲什麼它崩潰。我用同樣的方法從其他表中獲取數據,它給了我正確的數據。 我不知道它在說什麼語法錯誤03-25 16:43:39.420: E/SQLiteLog(1789): (1) near "FROM": syntax error 請大家告訴我如何解決這個問題。 在此先感謝。

+0

您的查詢在哪裏?請顯示您的查詢。 – GrIsHu

+0

@GrIsHu我已經給出了上面的查詢適配器類 '公共遊標returnData_schedule() { return db.query(TABLE_SCHEDULE,new String [] {date,time,food},null,null,null,null,null) ; }' – SSMA

回答

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

在這裏data,time,food應該是列名稱而不是值。當food是空字符串時,會出現語法錯誤。

我不知道你的表模式,但猜測他們是列名,應改爲:

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

帶引號爲字符串的列名。

+0

謝謝 實際上,這是團隊合作,我的團隊成員創建了變量並給他們錯誤的值。 感謝您的回覆,我解決了這個問題。 – SSMA

相關問題