Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to create a table with sqlite in Kotlin

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading