-1

I can insert rows in SQlite DB but when I try to retrieve the row from SQlite, my app stops working. Here is the code

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

can someone tell where is the actual problem ?

EDIT: The text I copied from android monitor, So finally I found the exception, here is it.

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)
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • You need to provide the error you are getting. Check Android Studio monitor for the stack trace. – Juan Oct 08 '17 at 22:26
  • After the app crashes you should see an exception and a stack trace. Please just post that part. – Juan Oct 08 '17 at 22:36
  • The exception is saying that the table doesn't exist. That can happen because you haven't actually created it, or because it is spelled wrongly. – Juan Oct 09 '17 at 00:44

2 Answers2

0

The error says No such table: Announcement, meaning you didn't create any table with a name Announcement

try to create a table Announcement in your database class like this.

@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)"
    );
}
dotGitignore
  • 1,597
  • 2
  • 15
  • 34
-1

I think your third argument in query is wrong. Check this document

Vidya Sagar
  • 170
  • 1
  • 16
  • So I tried this Cursor cursor = db.query(Announcement.TABLE, new String[] {Announcement.KEY_Announcement} , Announcement.KEY_Announcement + "=?", new String[] { }, null, null, null, null); but still getting error, I have posted exception aboove – bs140200456 Aqsa Anum Oct 08 '17 at 23:06
  • If using ? (placeholder) then 4th argument needs to be be the value that is to replace the placeholder. In your case a valid KEY_Announcement. The two (3rd an 4th args) basically result in a WHERE clause ie `WHERE KEY_Announcment = yourkeyannouncementvalue`. Whilst you are passing null. **However**, the log indicates that table Announcement isn't a table in the database. Would have to see the code for the `DBhelper` and also where the instance dbHelper is set. Perhaps try adding dbHelper = new dbHelper(this); immediately before the line `SQLiteDatabase db = dbHelper.getReadableDatabase();`. – MikeT Oct 08 '17 at 23:25