Nidhin_toms Nidhin_toms - 22 days ago 8
Android Question

Issue when initializing a table in Android

package nidhin.survey;

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

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

public class DatabaseHelper extends SQLiteOpenHelper {

static final String TABLE = "mygrades";
static final String NAME = "name";
static final String VALUE1 = "value1";
static final String VALUE2 = "value2";
static final String VALUE3 = "value3";
static final String VALUE4 = "value4";
static final String VALUE5 = "value5";
static final String VALUE6 = "value6";
static final String VALUE7 = "value7";
static final String VALUE8 = "value8";
static final String VALUE9 = "value9";
static final String VALUE10 = "value10";
static final String VALUE11 = "value11";


DatabaseHelper(Context context)
{
super(context, "grades.sqlite", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db)
{

db.execSQL("CREATE TABLE " + TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
VALUE1 + " TEXT, " + VALUE2 + " TEXT, " +VALUE3 + " TEXT, " + VALUE4 + " TEXT, " +VALUE5 + " TEXT, " +VALUE6 + " TEXT, " +VALUE7 + " TEXT, " +VALUE8 + " TEXT, " +VALUE9 + " TEXT, " +VALUE10 + " TEXT, " +
VALUE11 + " REAL);");

ContentValues cv = new ContentValues();

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
android.util.Log.w("mygrades", "Upgrading database, this will destroy all data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
}

}


I have a problem (which is likely to be a syntax issue). I am trying to create a table in android with the help of SQLite. I have a NAME column and VALUE1 through VALUE11 columns. However when I try to run the code, I get the following error in Logcat:

03-30 02:38:16.593: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhin.survey/nidhin.survey.Database}: android.database.sqlite.SQLiteException: no such column: value1: , while compiling: SELECT _id, name, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11 FROM mygrades


Any ideas ?

Answer

Have you modified your table schema since the first time you ran your app?

If you did, that is most likely your issue.

Looking at the code you gave, you still have the DB version set at 1, so the table has not been re-created with your new column additions (one of which I would bet would be "value1").

Change super(context, "grades.sqlite", null, 1); to super(context, "grades.sqlite", null, 2);, run your app again and I bet it will work out.