1

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.

5
  • 1
    Uninstall the app so that any old version of the database file is removed. Commented Jan 12, 2014 at 18:01
  • How and application can be uninstalled from an emulator? Commented Jan 12, 2014 at 18:04
  • Use app manager in settings, or adb uninstall <packagename> on command line. Commented Jan 12, 2014 at 18:07
  • @laalto I guess that worked. Its not showing any error now. Could you please tell me exactly what cause this error? Commented Jan 12, 2014 at 18:15
  • Uninstall application have the same effect that onUpdate, both recreate database. Commented Jan 12, 2014 at 18:19

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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);

}

4 Comments

Won't help as onUpgrade() is not implemented.
@ramaral : I have created all the fields at a time. Not altered after creating.
Have you changed KEY_PRICE column name?
Probably I added a ",".
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.