Skip to content Skip to sidebar Skip to footer

Error In Inserting Values To Database

I am creating a simple Database to add the values of product. While adding the entries in database I am getting an error in Logcat and the program get stop there and then. I am not

Solution 1:

SQLiteOpenHelperonCreate() is only called if the database file does not exist. If you modify the SQL in onCreate(), you'll have to ensure the database file is updated.

Two approaches:

  • Delete the old version of the database. Uninstall is one way to do this. This way the database is created with whatever code you currently have in onCreate(). This is often the simplest way during app development.

  • Bump up the database version number you pass to SQLiteOpenHelper superclass. If this number is different from the version number stored in the database file, onUpgrade() or onDowngrade() is called, and you can update the database schema. This is the preferred way when you already have released versions out so your users can preserve their data when updating your app.

Solution 2:

Delete your database from terminal

adb shell
cd /data/data/com.example.applicationname/databases
rm *

First you created table Books with x number of columns but pPrice column was not included in that create table query. Later on you added this column name to your create table query. That's why this problem happened.

Try to delete the database. It will delete the old database from application and when you re start new database will be created. onCreate() is only called if your database DOES NOT exist

Solution 3:

Seems that your database was created without column KEY_PRICE. After that you have altered your code adding column KEY_PRICE to String create.

If this is true you must increment database version in order it be created again:

Change:

CreateTable.DB_VERSION = 1;

To

CreateTable.DB_VERSION = 2;  

As laalto suggested change onUpgrade

@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + CreateTable.TABLE_NAME);
    onCreate(db);

}

Solution 4:

Check if you are missing some space. I had same problem , and solved with adding space...

Post a Comment for "Error In Inserting Values To Database"