-1

Hello I'm trying to get data from database but whenever I try this without try and catch app crashs. I tried if(cursor.moveToFirst()) structure but it didn't worked .

If I use cursor.moveToFirst() it's kinda work but this time database can't save objects.

This is my getdata function

      fun getdata() {


          val db = context?.openOrCreateDatabase("DRUGS", Context.MODE_PRIVATE, null)

          val cursor = db?.rawQuery("SELECT * FROM drugs", null)

      if (cursor != null) {
            

              val drugnameIx = cursor.getColumnIndex("drugname")
              val drugtimeIx = cursor.getColumnIndex("drugtime")
              val drugpieceIx = cursor.getColumnIndex("drugpiece")
              val drugidIx = cursor.getColumnIndex("id")
              val maindrugpieceIx = cursor.getColumnIndex("maindrugpiece")

              while (cursor.moveToNext()) {

                  ModelList.add(
                          DrugModel(
                                  cursor.getInt(drugidIx),
                                  cursor.getString(drugnameIx),
                                  cursor.getString(drugtimeIx),
                                  cursor.getIntOrNull(drugpieceIx)!!,
                                  cursor.getIntOrNull(maindrugpieceIx)!!



                          )
                  )


              }


              cursor?.close()


          }
      }

This is my error

2021-04-07 04:21:52.017 16650-16650/com.tunahan.maindrugreminder E/SQLiteLog: (1) no such table: 
drugs
2021-04-07 04:21:52.021 16650-16650/com.tunahan.maindrugreminder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tunahan.maindrugreminder, PID: 16650
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tunahan.maindrugreminder/com.tunahan.maindrugreminder.MainActivity}: android.database.sqlite.SQLiteException: no such table: drugs (code 1): , while compiling: SELECT * FROM drugs
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    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 table: drugs (code 1): , while compiling: SELECT * FROM drugs
    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 com.tunahan.maindrugreminder.MainFragment.getdata(MainFragment.kt:78)
    at com.tunahan.maindrugreminder.MainFragment.onViewCreated(MainFragment.kt:59)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332)
 
dltayn
  • 51
  • 2
  • 9
  • Probably your DBMS (SQLite) is case-sensitive for table names on Android (like [MySQL is case-sensetive for it](https://stackoverflow.com/questions/6134006/are-table-names-in-mysql-case-sensitive) on most variants of Unix). Will it work if you change your query to `SELECT * FROM DRUGS`? Because you create it this way - in upper-case. – Михаил Нафталь Apr 07 '21 at 10:49

1 Answers1

1

Your exception tells you the problem: no such table: drugs

While you have a database named drugs, from your code it doesn't seem like there is a table with that name in it.

F43nd1r
  • 7,690
  • 3
  • 24
  • 62