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
>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)}")