biscuitstack biscuitstack - 1 year ago 46
Android Question

Passing an SQLite database delete function a table name as a String in Android

If I run:

void deleteTableRecords() {
SQLiteDatabase db = this.getWritableDatabase();

I get my intended result. However, if I run:

void deleteTableRecords(String tableName) {
SQLiteDatabase db = this.getWritableDatabase();

with a call of
I get a crash and told that:

android.database.sqlite.SQLiteException: no such table: TABLE_ALBUMS

What should I be passing a table name as if not a string?

Answer Source

TABLE_ALBUMS is the name of your variable. Its value is most probably something else.

I guess you assigned a value to TABLE_ALBUMS, for example:

private static final String TABLE_ALBUMS = "someValue";

In this case you need to pass the actual value, not the name of the variable:


A better approach is to make it a public constant:

public class DbHelper {
    public static final String TABLE_ALBUMS = "someValue";

    // ...


So you can access it in a static way: