Question

I created a database using SQLite, I seem to create the database correctly, spaces and all, but when i run the app the exception i get is it can't find the column id: "_id"

Please review my code and let me know what I am doing wrong.

public static final String KEY_ROW_ID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String RATE = "rate_it";

private static final String DATABASE_NAME = "iDRateIt";
private static final String DATABASE_TABLE = "tblPeople";
private static final int DATABASE_VERSION = 1;

private dbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class dbHelper extends SQLiteOpenHelper {

    public dbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " +
                KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                RATE + " TEXT NOT NULL);"

                );
    }

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

}

//Constructor
public RateIt (Context c) {
    ourContext = c;
}

public RateIt open() throws SQLException
{
    ourHelper = new dbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
//Close Database
public void close()
{
    ourHelper.close();
}

//Insert Method
public long createEntry(String name, String rate) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();

    cv.put(KEY_NAME, name);
    cv.put(RATE, rate);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);


}

public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROW_ID, KEY_NAME, RATE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    String result = "";

    int iRow = c.getColumnIndex(KEY_ROW_ID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iRate = c.getColumnIndex(RATE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        result = result + c.getString(iRow) + " " + c.getString(iName) 
                + " " + c.getString(iRate) + "\n";

    }

    return result;

}

}

Was it helpful?

Solution

It looks like there is an issue with

c.getString(iRow)

from your code the _id column is defined as an integer

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " +
            KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 

You would need to treat this a an integer

int iRow = (c.getLong(c.getColumnIndex(KEY_ROW_ID))); or

int iRow = (c.getInteger(c.getColumnIndex(KEY_ROW_ID)));

Hope this helps.

Also, you have defined Rate

RATE + " TEXT NOT NULL

as a text field, if your doing this just for test purposes fine, but good database design would define this as a numeric field of some type, i.e. Integer, Long.

Have a great day.

OTHER TIPS

Are you sure its creating it properly? Because

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " +
        KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_NAME + " TEXT NOT NULL, " +
        RATE + " TEXT NOT NULL);"

appears to have a stray semicolon in at and should be:

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " +
        KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_NAME + " TEXT NOT NULL, " +
        RATE + " TEXT NOT NULL)"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top