Salman9
Salman9

Reputation: 265

SQLite exception column does not exist

I am trying to write a simple application to just count the numbers of times I press on a button using SQLite. Would have only one table with two columns. I have the SQLiteOpenHelper implemented as follows:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqliteHelperInstance extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "app_stats";
public static final String TABLE_NAME = "day_stats";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_CIGCOUNT = "cigcount";
public static final int DATABASE_VERSION = 1;

private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " +  COLUMN_DATE + " text "
+ COLUMN_CIGCOUNT + " integer );";

public SqliteHelperInstance(Context context){
    super(context,DATABASE_NAME , null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_DB);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

}

So I have a table with two columns first is "date" next is a counter. Now using my database manager class I create a method to insert into my table.

public class DbManager {

private SqliteHelperInstance sqlHelper;
private SQLiteDatabase database;
    public DbManager(Context context){
    sqlHelper = new SqliteHelperInstance(context);
}
    public void open() throws SQLException{
    database = sqlHelper.getWritableDatabase();
}
    public void addEntry(String dateEntry){
            DbEntry newEntry = new DbEntry();
            newEntry.setDate(dateEntry);
            newEntry.setCounter(0);
    values.put(SqliteHelperInstance.COLUMN_DATE, dateEntry);
    values.put(SqliteHelperInstance.COLUMN_CIGCOUNT, newEntry.getCigNum());
    database.insert(SqliteHelperInstance.TABLE_NAME, null, values);
    }
}

But when I run my application it gives me an error that column "cigcount" does not exist. Here is the catlog message:

05-08 16:23:15.830: E/SQLiteLog(1076): (1) table day_stats has no column named cigcount
05-08 16:23:15.850: E/SQLiteDatabase(1076): Error inserting cigcount=0 date=2014-05-08
05-08 16:23:15.850: E/SQLiteDatabase(1076): android.database.sqlite.SQLiteException: table day_stats has no column named cigcount (code 1): , while compiling: INSERT INTO day_stats(cigcount,date) VALUES (?,?)

Can anyone help me with this problem?

Upvotes: 0

Views: 268

Answers (1)

laalto
laalto

Reputation: 152787

There's a comma , missing between your column specifications:

private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " +  COLUMN_DATE + " text "
+ COLUMN_CIGCOUNT + " integer );";

should be

private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " +  COLUMN_DATE + " text, "
+ COLUMN_CIGCOUNT + " integer );";

After fixing it, uninstall your app so the old database file is removed. (You have a syntax error in your onUpgrade() as well and incrementing the version alone won't work to update the schema.)

Upvotes: 4

Related Questions