我是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 :(得分: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);
列名称引用为字符串。