0

I am developing an app where I want to export the database tables as an excel/csv file.

Here is my current code:

val dbhelper = DBHelper()
val exportDir = File(getExternalStorageDirectory(), "")
if (!exportDir.exists()) {
    exportDir.mkdirs()
}

val file = File(exportDir, "csvname.csv")
try {
    file.createNewFile()
    val csvWrite = CSVWriter(FileWriter(file))
    val db = dbhelper.readableDatabase
    val curCSV = db.rawQuery("SELECT * FROM TableName", null)
    csvWrite.writeNext(curCSV.columnNames)
    while (curCSV.moveToNext()) {
        //Which column you want to export
        val arrStr = arrayOf(curCSV.getString(0), curCSV.getString(1), curCSV.getString(2))
        csvWrite.writeNext(arrStr)
    }
    csvWrite.close()
    curCSV.close()
} catch (sqlEx: java.lang.Exception) {
    Log.e("MainActivity", sqlEx.message, sqlEx)
}

Now, I am currently facing an error that says the table I indicated in the rawQuery() does not exist. I tried to download the database and view it in DB Browser and I can see the table.

Here is the error I am encountering:

android.database.sqlite.SQLiteException: no such table: TableName (code 1): , while compiling: SELECT * FROM TableName

Upon checking the database name, it seems like DBHelper() has a default value of keystore.db

How can I change the default value of the database name that is being called by DBHelper()?

Angel
  • 175
  • 1
  • 3
  • 10
  • You haven't created the table in DB, isn't it? – Raptor Aug 18 '23 at 04:25
  • @Raptor I did. I just used DB Browser to verify if the table is actually there. – Angel Aug 18 '23 at 04:33
  • Add something along the lines of `val showcsr = db.rawQuery("SELECT * FROM sqlite_master;",null)`, followed by `DatabaseUtils.dumpCursor(showcsr)`. Add these 2 lines before your rawQuery. Run and examine the dumped output in the log. This will tell you all the tables, indexes etc. If still confused edit the question to include the output. You may want to refer to https://developer.android.com/reference/android/database/DatabaseUtils#dumpCursor(android.database.Cursor) – MikeT Aug 18 '23 at 09:10
  • We may be able to help you better if you [edit] your question to show us the code of your `DBHelper()` function. – O. Jones Aug 18 '23 at 10:51

0 Answers0