从Sqlite读取行时,应用程序停止

时间:2017-10-08 22:23:18

标签: android sqlite

我可以在SQlite DB中插入行但是当我尝试从SQlite检索行时,我的应用程序停止工作。这是代码

String model;
    public String getDataFromDB(){
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.query(Announcement.TABLE, new String[] {Announcement.KEY_Announcement}
                , Announcement.KEY_Announcement,
                null, null, null, null, null);

        if (cursor.moveToFirst()){

                model = cursor.getString(cursor.getColumnIndex(Announcement.KEY_Announcement));

        }

        return model;
    }

有人能说出实际问题在哪里吗?

编辑:我从android监视器复制的文本, 所以最后我找到了例外,就是这样。

00-09 03:52:12.610 335-335/com.example.anum.lmslite E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anum.lmslite/com.example.anum.lmslite.Announcement_Activities.CS101_Announcement}: android.database.sqlite.SQLiteException: no such table: Announcement (code 1): , while compiling: SELECT  * FROM Announcement
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:162)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                          at android.os.Looper.loop(Looper.java:194)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5392)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:525)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:838)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: android.database.sqlite.SQLiteException: no such table: Announcement (code 1): , while compiling: SELECT  * FROM Announcement
                                                                          at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                          at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
                                                                          at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
                                                                          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:44)
                                                                          at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
                                                                          at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
                                                                          at com.example.anum.lmslite.StudentRepo.getDataFromDB(StudentRepo.java:91)
                                                                          at com.example.anum.lmslite.Announcement_Activities.CS101_Announcement.onCreate(CS101_Announcement.java:46)
                                                                          at android.app.Activity.performCreate(Activity.java:5122)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:162) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                                          at android.os.Looper.loop(Looper.java:194) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5392) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:838) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
                                                                          at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

  

错误说没有这样的表:公告,这意味着你没有创建   任何名称为公告

的表格      

尝试在您的数据库类中创建一个表公告,如下所示。

@Override
public void onCreate(SQLiteDatabase db) {
    // Your code goes here to create a table Announcement
    db.execSQL("CREATE TABLE IF NOT EXISTS Announcement ("
       + "_id INTEGER PRIMARY KEY AUTOINCREMENT "
       + " COLUMN_ONE TEXT, "
       + " COLUMN_TWO TEXT)"
    );
}

答案 1 :(得分:-1)

我认为查询中的第三个参数是错误的。 请检查此document