I am trying to create boardgamegeek APIs based app in AndroidStudio, however from unknown reason I m getting my database created with no columns. Here s Logical log:
(1) no such table: games in "INSERT INTO games(release_year,bgg_id,game_title,thumbnail,original_title) VALUES (?,?,?,?,?)"
Class which implements database + the insertion method:
class DatabaseInit(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
companion object {
val DATABASE_VERSION = 2
val DATABASE_NAME = "boardgames.db"
// Tabela z informacjami o grach
val TABLE_GAMES = "games"
val COLUMN_GAME_TITLE = "game_title"
val COLUMN_ORIGINAL_TITLE = "original_title"
val COLUMN_RELEASE_YEAR = "release_year"
val COLUMN_BGG_ID = "bgg_id"
val COLUMN_TYPE = "type"
val COLUMN_THUMBNAIL = "thumbnail"
}
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("DROP TABLE IF EXISTS `$TABLE_GAMES`")
val CREATE_TABLE_GAMES = "CREATE TABLE $TABLE_GAMES(" +
"$COLUMN_GAME_TITLE TEXT," +
"$COLUMN_ORIGINAL_TITLE TEXT," +
"$COLUMN_RELEASE_YEAR INTEGER," +
"$COLUMN_BGG_ID INTEGER PRIMARY KEY," +
"$COLUMN_TYPE INTEGER," +
"$COLUMN_THUMBNAIL BLOB)"
db.execSQL(CREATE_TABLE_GAMES)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS $TABLE_GAMES")
onCreate(db)
}
fun addDataFromXml(context: Context) {
val xmlFile = File(context.filesDir, "XML/dane.xml")
val xmlInputStream = FileInputStream(xmlFile)
val xmlParser = Xml.newPullParser()
xmlParser.setInput(xmlInputStream, null)
var eventType = xmlParser.eventType
var gameId = ""
var gameType = -1
var gameTitle = ""
var releaseYear = ""
var thumbnail = ""
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && xmlParser.name == "item") {
gameId = xmlParser.getAttributeValue(null, "objectid")
val subtype = xmlParser.getAttributeValue(null, "subtype")
gameType = when (subtype) {
"boardgame" -> 1
"boardgameexpansion" -> 0
else -> -1
}
} else if (eventType == XmlPullParser.START_TAG && xmlParser.name == "name") {
gameTitle = xmlParser.nextText()
} else if (eventType == XmlPullParser.START_TAG && xmlParser.name == "yearpublished") {
releaseYear = xmlParser.nextText()
} else if (eventType == XmlPullParser.START_TAG && xmlParser.name == "thumbnail") {
thumbnail = xmlParser.nextText()
} else if (eventType == XmlPullParser.END_TAG && xmlParser.name == "item") {
// Dodawanie danych do bazy danych
val values = ContentValues().apply {
put(COLUMN_GAME_TITLE, gameTitle)
put(COLUMN_ORIGINAL_TITLE, "")
put(COLUMN_RELEASE_YEAR, releaseYear.toInt())
put(COLUMN_BGG_ID, gameId.toInt())
put(COLUMN_THUMBNAIL, thumbnail)
}
val db = writableDatabase
db.insert(TABLE_GAMES, null, values)
db.close()
}
eventType = xmlParser.next()
}
xmlInputStream.close()
}
}
The insertion method is being called from another class, however that is not the case here since the table is being created empty from onCreate method.
The following permissions have been added to androidmanifest:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I ve done several attempts to make it work which include:
- Manual database removal from emulator files
- DATABASE_VERSION increase from 1 to 2
- Droping string interpolation and replacing it with plain text
val CREATE_TABLE_GAMES = "CREATE TABLE $TABLE_GAMES(" +
"$COLUMN_GAME_TITLE TEXT," +
"$COLUMN_ORIGINAL_TITLE TEXT," +
"$COLUMN_RELEASE_YEAR INTEGER," +
"$COLUMN_BGG_ID INTEGER PRIMARY KEY," +
"$COLUMN_TYPE INTEGER," +
"$COLUMN_THUMBNAIL BLOB)"
- Following code reformat:
class DatabaseInit(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
companion object {
const val DATABASE_VERSION = 1
const val DATABASE_NAME = "boardgames.db"
const val TABLE_GAMES = "games"
const val COLUMN_GAME_TITLE = "game_title"
const val COLUMN_ORIGINAL_TITLE = "original_title"
const val COLUMN_RELEASE_YEAR = "release_year"
const val COLUMN_BGG_ID = "bgg_id"
const val COLUMN_TYPE = "type"
const val COLUMN_THUMBNAIL = "thumbnail"
private const val CREATE_TABLE_GAMES = "CREATE TABLE $TABLE_GAMES (" +
"$COLUMN_GAME_TITLE TEXT," +
"$COLUMN_ORIGINAL_TITLE TEXT," +
"$COLUMN_RELEASE_YEAR INTEGER," +
"$COLUMN_BGG_ID INTEGER PRIMARY KEY," +
"$COLUMN_TYPE INTEGER," +
"$COLUMN_THUMBNAIL BLOB)"
}
override fun onCreate(db: SQLiteDatabase) {
db.execSQL(CREATE_TABLE_GAMES)
}
//remaining code without changes
My another idea was that the programs cannot see that database, however creating it empty make me leave it.
Thanks in advance for any tips how to resolve my issue.