无法从表中获取数据:编译时出错

时间:2017-11-17 12:44:01

标签: android listview

我的应用程序必须从DataBase获取一些信息并在ListView中显示一个表。 所以基本上,没有专门用于向表中插入数据的Activity。 而我正在ModelHelper中这样做: 用户表包含:ID,Fname,Lname; ID为PRIMARY KEY AUTOINCREMENT NOT NULL;

public void onCreate(SQLiteDatabase db) {
       db.execSQL(CREATE_TABLE_USER);
db.execSQL("INSERT INTO " + USER +" VALUES ('DOME','TRY')");
}

我做了一个返回用户Fnames的方法

public ArrayList getOnlyNames(){
        ArrayList lst = new ArrayList();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("SELECT " +KEY_FNAME+ " from " +TABLE_QUESTION,null);
        if (c.moveToFirst()) {
            while(c.isAfterLast()==false){
                String t1 = c.getString(1);
                lst.add(t1);
                c.moveToNext();
            }
        }
        return lst;
    }

在我宣布列表的活动中: ListView lst =(Listview)findViewById(R.id.list1);

这是使用过的方法:

public void showQuest(View view){

        ArrayList<String> lstQ = md.getOnlyNames();
        ArrayAdapter lstad = new ArrayAdapter(this, android.R.layout.simple_list_item_1,lstQ);
        lst.setAdapter(lstad);
    }

我在同一活动的按钮中添加了Onlick“showquest”。

日志错误是:

FATAL EXCEPTION: main
                                                                                Process: com.example.root.myapplication, PID: 10225
                                                                                java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                    at android.view.View.performClick(View.java:6294)
                                                                                    at android.view.View$PerformClick.run(View.java:24770)
                                                                                    at android.os.Handler.handleCallback(Handler.java:790)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                    at android.view.View.performClick(View.java:6294) 
                                                                                    at android.view.View$PerformClick.run(View.java:24770) 
                                                                                    at android.os.Handler.handleCallback(Handler.java:790) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                    at android.os.Looper.loop(Looper.java:164) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
                                                                                 Caused by: android.database.sqlite.SQLiteException: no such column: Question (code 1): , while compiling: SELECT Question from Question
                                                                                    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
                                                                                    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
                                                                                    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
                                                                                    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1392)
                                                                                    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
                                                                                    at model.sqlite.data.ModelHelper.getOnlyQuestions(ModelHelper.java:253)
                                                                                    at com.example.root.myapplication.kamiActivity.showQuest(kamiActivity.java:98)
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                    at android.view.View.performClick(View.java:6294) 
                                                                                    at android.view.View$PerformClick.run(View.java:24770) 
                                                                                    at android.os.Handler.handleCallback(Handler.java:790) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                    at android.os.Looper.loop(Looper.java:164) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6494)

问题来自按钮使用该方法,我认为数据库是空的,但我不知道为什么它不能采用该列,它已经在表中。

1 个答案:

答案 0 :(得分:0)

您只需要一列的数据。列索引为0而不是1。

while(c.isAfterLast()==false){
            String t1 = c.getString(0);
            lst.add(t1);
            c.moveToNext();
}

例外说明了一切。

Caused by: android.database.sqlite.SQLiteException: no such column: Question (code 1): , while compiling: SELECT Question from Question

安全方法

c.getString(c.getColumnIndex(KEY_NAME));