Home > front end >  I am trying to create a table with sqlite in Kotlin
I am trying to create a table with sqlite in Kotlin

Time:08-29

I am trying to create a table with sqlite in Kotlin. I'm trying to keep a string type data in the table but it says 0, what should I do? I used VARCHAR , NVARCHAR , TEXT and the result did not change.

  try{
        val veritabani=this.openOrCreateDatabase("Urunler", Context.MODE_PRIVATE,null)
        veritabani.execSQL("CREATE TABLE IF NOT EXISTS urunler (id INTEGER PRIMARY KEY, isim TEXT , fiyat INT) " )
         veritabani.execSQL( "INSERT INTO urunler (isim, fiyat ) VALUES ('asda',100)")

        val cursor =veritabani.rawQuery("SELECT * FROM urunler " ,null)


        val idColumnIndex=cursor.getColumnIndex("id")
        val isimColumnIndex=cursor.getColumnIndex("isim")
        val fiyatColumnIndex=cursor.getColumnIndex("fiyat")


        System.out.println("deneme11")

        while(cursor.moveToNext()){
            System.out.println("ID :${cursor.getInt(idColumnIndex)}")
            System.out.println("isim :${cursor.getInt(isimColumnIndex)}")
            System.out.println("fiyat :${cursor.getInt(fiyatColumnIndex)}")

        }

    }
    catch (e:Exception){


        System.out.println("error"  e.message);
    }

output is like this

I/System.out: ID :1
I/System.out: isim :0
I/System.out: fiyat :100

CodePudding user response:

Your code tries to retrieve an integer because you use getInt() to get the value of the column isim.

You should use getString():

System.out.println("isim :${cursor.getString(isimColumnIndex)}")

CodePudding user response:

I would like to suggest you to use Room database, if you want to use the database in old application, then you can use old dependency of Room.

 def room_version = "1.1.1"

implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
testImplementation "android.arch.persistence.room:testing:$room_version"
  • Related