lotdrops
lotdrops

Reputation: 320

Android SQLiteException no such column

I get the error:

android.database.sqlite.SQLiteException: no such column: app_table (code 1): , while compiling: SELECT _id FROM apps WHERE app_table=? AND app_row=? AND app_column=?

The "select" is called in this part of the code:

public int getKeyId(int table, int row, int column) {
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.query(DB_TABLE, new String[] { KEY_ID }, KEY_TABLE+ "=?" + " AND "
            + KEY_ROW+ "=?" + " AND "+ KEY_COLUMN+ "=?",
            new String[] { String.valueOf(table), String.valueOf(row),
            String.valueOf(column)}, null, null, null, null );
    if (cursor.getCount()==0) {
        return -1;
    }
    return cursor.getInt(0);
}

These are the strings I use to create the database:

private static final String TAG = "SQLiteOpenHelper";
private static final String DB_NAME = "apps_tables";
private static final int DB_VERSION = 1;
private static final String DB_TABLE = "apps";
/**
 * Columns
 */
private static final String KEY_ID = "_id";
private static final String KEY_TABLE = "app_table";
private static final String KEY_ROW = "app_row";
private static final String KEY_COLUMN = "app_column";
private static final String KEY_NAME = "app_name";

/**
 * Creation statement
 */
private static final String DB_CREATE = "CREATE TABLE " + DB_TABLE + " (" + KEY_ID +
        " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TABLE + " INTEGER NOT NULL, "
        + KEY_ROW + " INTEGER NOT NULL, " + KEY_COLUMN + " INTEGER NOT NULL, " +
        KEY_NAME +" TEXT NOT NULL);";

onCreate calls:

database.execSQL(DB_CREATE);

Does anyone know why do I get this error? Thanks!

Upvotes: 0

Views: 299

Answers (2)

Ratul Ghosh
Ratul Ghosh

Reputation: 1500

Go to settings clear the app data and then run again .

This problem was occuring because when app in updated or resintsalled databse doesn't get deleted by android. In that case SQLiteOpenHelper doesn't call onCreate(SQLiteDatabase db), rather it check for database version update . you neither have updated your database version nor written onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) method properly so that column doesn't exist in previous database. When you clear teh app data, database removed, then when you lauch the app again , it again called the onCreate(SQLiteDatabase db), so your create sql run once again and you dont get that exception. The actual way of do this is increase your database version and use onUpgrade method to alter the database change.

For the devlopment purpose you can use the clear app data, but in production make sure you upgared Database Version and make use of onUpgrade method.

Upvotes: 4

No such column error means you got success in creation of table but not in creation of column of that table so as per understanding

create table command get executed but i think you are column not get formed so please check out your query run this query in sqlite browesr check this is working fine or not?

private static final String DB_CREATE = "CREATE TABLE " + DB_TABLE + " (" + KEY_ID +
        " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TABLE + " INTEGER NOT NULL, "
        + KEY_ROW + " INTEGER NOT NULL, " + KEY_COLUMN + " INTEGER NOT NULL, " +
        KEY_NAME +" TEXT NOT NULL);";

your error is coming due to these lines

check into DDMS DATA>DATA folder that table is existing over there ?

try out this code too into your onCreate Method of sqliteopnhelper class

public void onCreate(SQLiteDatabase db) {

db.execSQL(" CREATE TABLE " + DB_TABLE + " (" +
        KEY_ID + "  INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_TABLE + " INTEGER NOT NULL, " +
        KEY_ROW + " INTEGER NOT NULL, " +
        KEY_COLUMN + " INTEGER NOT NULL, " +
        KEY_NAME + " INTEGER NOT NULL);");
}

Upvotes: 0

Related Questions