Android 应用程序从 SQLite 获取数据

Posted

技术标签:

【中文标题】Android 应用程序从 SQLite 获取数据【英文标题】:Android application fetch data from SQLite 【发布时间】:2014-03-25 11:47:12 【问题描述】:

我是 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;
    


这是日志猫

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 ComponentInfocom.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 中谈论的是什么语法错误 请大家告诉我如何解决这个问题。 提前致谢。

【问题讨论】:

您的查询在哪里?请出示您的查询。 @GrIsHu 我已经在适配器类public Cursor returnData_schedule() return db.query(TABLE_SCHEDULE,new String[] date,time,food,null,null,null,null,null); 上面给出了查询 【参考方案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);

将列名引用为字符串。

【讨论】:

谢谢 其实这是团队合作,我的团队成员创建了变量并给他们错误的值。感谢您的回复,我解决了这个问题。

以上是关于Android 应用程序从 SQLite 获取数据的主要内容,如果未能解决你的问题,请参考以下文章

从在线数据库同步后从 android 应用程序获取 sqlite 数据库

在 Android 中从服务器数据库获取数据到 Sqlite 数据库

从android数据库sqlite中获取特定数据

Android 从 Sqlite 数据库保存和获取图像

如何从android中的sqlite中检索数据

如何从 Sqlite ( android ) 中的 datetime 字段获取日期