Saggy
Saggy

Reputation: 624

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 clear with the error but its something related to insertion of data or in query I have written. I tried all possible alternatives I could.

Program Code is :

DataBase.java

public class DataBase extends SQLiteOpenHelper
{

    public DataBase(Context context) {
        super(context, CreateTable.DB_NAME, null, CreateTable.DB_VERSION);
        // TODO Auto-generated constructor stub
    }

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

        String create = "Create Table " + CreateTable.TABLE_NAME + "( " + CreateTable.KEY_ID
                                        + " INTEGER PRIMARY KEY," + CreateTable.KEY_NAME + " TEXT,"
                                        + CreateTable.KEY_PRICE + " REAL)";

        db.execSQL(create);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub


    }

    public void addProduct(Product p)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues c = new ContentValues();
        c.put(CreateTable.KEY_NAME, p.getName());
        c.put(CreateTable.KEY_PRICE, p.getPrice());

        db.insert(CreateTable.TABLE_NAME, null, c);
        db.close();
    }

}

EnterDeatils.java

public class EnterDeatils extends Activity {

    EditText name;
    EditText price;
    Button done;
    int id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter_deatils);

        name = (EditText) findViewById(R.id.edtname);
        price = (EditText) findViewById(R.id.edtprice);
        done = (Button) findViewById(R.id.btndone);

        done.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Product p = new Product();
                p.setId(id);
                p.setName(name.getText().toString());
                p.setPrice(Float.valueOf(price.getText().toString()));

                DataBase d = new DataBase(EnterDeatils.this);
                d.addProduct(p);
            }
        });
    }

}

LogCat Error:

01-12 23:06:52.343: E/Database(382): Error inserting pPrice=12.0 pName=ds
01-12 23:06:52.343: E/Database(382): android.database.sqlite.SQLiteException: table Books has no column named pPrice: , while compiling: INSERT INTO Books(pPrice, pName) VALUES(?, ?);

Requesting you guys to just help me to identify the error. Thanks in Advance.

Upvotes: 1

Views: 391

Answers (4)

Mladen Rakonjac
Mladen Rakonjac

Reputation: 9812

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

Upvotes: -1

Zohra Khan
Zohra Khan

Reputation: 5302

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

Upvotes: 2

ramaral
ramaral

Reputation: 6179

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

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

}

Upvotes: 1

laalto
laalto

Reputation: 152807

SQLiteOpenHelper onCreate() 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.

Upvotes: 2

Related Questions