0

I get this error when adding data to a table in my Sqlite database.

    12-21 17:31:19.345: E/SQLiteLog(24127): (1) no such table: accessory_data
    12-21 17:31:19.345: E/DB ERROR(24127): android.database.sqlite.SQLiteException: 
no such table: accessory_data (code 1): , 
while compiling: SELECT aid, name, image, url, price 
FROM accessory_data

Note that the Database is previously created by another method in order to add data to another table. What I'm doing here is simply adding data to a different table in the same database. Is that the root of the problem?

DBOperations Class:

public class DBOps extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "jezzadb";


    public String CREATE_ACC_QUERY = "CREATE TABLE " + AccessoriesTableInfo.TABLE_NAME + " (" + AccessoriesTableInfo.AID + " VARCHAR, " + 
            AccessoriesTableInfo.NAME + " VARCHAR, " + AccessoriesTableInfo.IMAGE + " VARCHAR, " + AccessoriesTableInfo.PRICE + " VARCHAR, " + 
            AccessoriesTableInfo.URL + " VARCHAR);";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_ACC_QUERY);
    }


public void insertAccessories(DBOps dop, Accessories a)
    {
        SQLiteDatabase SQ = dop.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(AccessoriesTableInfo.AID, a.ID);
        cv.put(AccessoriesTableInfo.NAME, a.Name);
        cv.put(AccessoriesTableInfo.URL, a.URL);
        cv.put(AccessoriesTableInfo.IMAGE, a.Image);
        cv.put(AccessoriesTableInfo.PRICE, a.PRICE);
        SQ.insert(AccessoriesTableInfo.TABLE_NAME, null, cv);
        Log.d("Database Operations", "Inserted Accessories succesfully!");
    }


    public ArrayList<Accessories> getAccessories(DBOps dop)
    {
        ArrayList<Accessories> arr = new ArrayList<Accessories>();

        SQLiteDatabase SQ = dop.getReadableDatabase();
        String[] columns = {AccessoriesTableInfo.AID,AccessoriesTableInfo.NAME,AccessoriesTableInfo.IMAGE,AccessoriesTableInfo.URL,
                AccessoriesTableInfo.PRICE};
        Cursor CR = SQ.query(AccessoriesTableInfo.TABLE_NAME, columns, null, null, null, null, null);
        CR.moveToFirst();

        for(int i=0;i<CR.getCount();i++)
        {
            while (!CR.isAfterLast()) {
                Accessories a= new Accessories();
                a.ID = CR.getString(0);
                a.Name = CR.getString(1);
                a.URL = CR.getString(2);
                a.Image = CR.getString(3);
                a.PRICE = CR.getString(4);

                arr.add(a);
                CR.moveToNext();
            }
            CR.close();
            break;
        }
return arr;
    }

AccessoriesTableInfo Class:

public class AccessoriesTableData {

    public AccessoriesTableData()
    {

    }
    public static abstract class AccessoriesTableInfo implements BaseColumns
    {
        public static final String AID = "aid";
        public static final String NAME = "name";
        public static final String IMAGE = "image";
        public static final String URL = "url";
        public static final String PRICE = "price";
        public static final String TABLE_NAME = "accessory_data";
    }
}

What am I doing wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pjayness
  • 365
  • 1
  • 6
  • 22

1 Answers1

1

Note that the Database is previously created by another method in order to add data to another table. What I'm doing here is simply adding data to a different table in the same database. Is that the root of the problem?

Yes, that's the problem. Use only one SQLiteOpenHelper subclass per database file.

What happens is that the database file with the requested version 1 already exists, so no database helper lifecycle callbacks like onCreate() or onUpgrade() get invoked.

Further reading: When is SQLiteOpenHelper onCreate() / onUpgrade() run?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
  • Oh I see.. but is there a way to add the new table and the new values without having to drop the existing tables or deleting the database? Is it the onUpgrade() method? – Pjayness Dec 21 '14 at 12:19
  • Change `private static final int DATABASE_VERSION = 1;` to `private static final int DATABASE_VERSION = 2;`. By doing so, `onUpgrade()` is called. – Phantômaxx Dec 21 '14 at 12:33