2

I have a CSV backup from tables. I want to import it to my SQLite database in Kotlin.

My database helper:

package com.freetux.db
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

val DATABASENAME = "SahatDB"

val TABLENAME = "t130m46ch"

class DataBaseHandler(var context: Context) : SQLiteOpenHelper(context, DATABASENAME, null,
    1) {
    override fun onCreate(db: SQLiteDatabase?) {
        val createTable = "CREATE TABLE " + TABLENAME + " (\n" +
                "    `range`\tINTEGER,\n" +
                "    `darajeh`\tINTEGER\n" +
                "    )"
        db?.execSQL(createTable)
    }
    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        //onCreate(db);
    }
    override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        onUpgrade(db, oldVersion, newVersion)
    }

    fun findDarajeh(charge: Int, range: String): Int? {

        val table = "t130m46ch$charge"
        val query =
            "SELECT darajeh FROM $table WHERE $range = ?"

        val db = this.readableDatabase
        var cursor: Cursor? = null
        
        cursor = db.rawQuery(query, arrayOf(range))
        var darajeh: Int?
        darajeh = Integer.parseInt(cursor.getString(2))
        cursor.close()
        db.close()
        return darajeh
    }

}

How I can import tables to SQLite database?

user4157124
  • 2,809
  • 13
  • 27
  • 42
freetux
  • 23
  • 3
  • First if you just start a new project you should take a look at [Room](https://developer.android.com/training/data-storage/room) moreover, you can check https://stackoverflow.com/questions/9544737/read-file-from-assets for details, then handle the logic by yourself to load the data in your database – axel7083 Sep 06 '22 at 15:16

0 Answers0