0

I am developing a shopping cart app.But got an error in adding item details to database, on clicking ADD TO CART button. Error log says there is no such database table.and force me to SHUTTING DOWN VM. Please help me out.

I have two database in this activity.problem is with add2cart table in addcart database

Product_Detail.java

public class Product_Details extends Activity{

TextView name,price,specification,feature
String nme;
SQLiteDatabase mydb;
String pname;
String prprice;
String pspec;
String pfeature;
Button add2cart,by_nw;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_dtls);
    image=(ImageView)findViewById(R.id.pr_img);
    name = (TextView) findViewById(R.id.txtPr_name);
    price = (TextView) findViewById(R.id.txtprice);
    specification=(TextView)findViewById(R.id.txtPr_spec);
    feature=(TextView)findViewById(R.id.txtPr_feature);
    add2cart=(Button)findViewById(R.id.add2cart);
    by_nw=(Button)findViewById(R.id.buy_nw);

    Intent in = getIntent();
    Bundle bn = in.getExtras();
    nme = bn.getString("key");
    mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
    mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(img BLOB,pnme varchar,prate varchar,pqty varchar,ptotl vachar)");
    mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null);
    Cursor cr = mydb.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);

    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pname"));
        String pr1price = cr.getString(cr.getColumnIndex("pprice"));
        String prspc=cr.getString(cr.getColumnIndex("pspec"));
        String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
        pname = name;
        prprice = pr1price;
        pspec=prspc;
        pfeature=prfeature;
    }
    name.setText(pname);
    price.setText("Rs " +prprice + "/-");
    specification.setText(pspec);
    feature.setText(pfeature);


    add2cart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String nm=name.getText().toString();
            String rate=price.getText().toString();
            mydb.execSQL("INSERT INTO add2cart VALUES('"+nm+"','"+rate+"')");
            Toast.makeText(getApplicationContext(),"add to cart",Toast.LENGTH_SHORT).show();
            Intent in=new Intent(Product_Details.this,add2cart.class);  
            startActivity(in);
        }
    });


    by_nw.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in=new Intent(Product_Details.this,buy_nw.class);
            startActivity(in);
        }
    });

}

}

2 Answers 2

1

I opened my addcart database in setOnClickListner and my code worked.

Problem was the place where i opened the addcart database.It consider only the last opened database in Main.

Corrected code

public class Product_Details extends Activity{

TextView name,price,specification,feature;
String nme;
SQLiteDatabase mydb;
String pname;
String prprice;
String pspec;
String pfeature;
Button add2cart,by_nw;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_dtls);
    image=(ImageView)findViewById(R.id.pr_img);
    name = (TextView) findViewById(R.id.txtPr_name);
    price = (TextView) findViewById(R.id.txtprice);
    specification=(TextView)findViewById(R.id.txtPr_spec);
    feature=(TextView)findViewById(R.id.txtPr_feature);
    add2cart=(Button)findViewById(R.id.add2cart);
    by_nw=(Button)findViewById(R.id.buy_nw);

    Intent in = getIntent();
    Bundle bn = in.getExtras();
    nme = bn.getString("key");


    mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null);
    Cursor cr = mydb.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);

    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pname"));
        String pr1price = cr.getString(cr.getColumnIndex("pprice"));
        String prspc=cr.getString(cr.getColumnIndex("pspec"));
        String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
        pname = name;
        prprice = pr1price;
        pspec=prspc;
        pfeature=prfeature;
    }
    name.setText(pname);
    price.setText("Rs " +prprice + "/-");
    specification.setText(pspec);
    feature.setText(pfeature);


    add2cart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String nm=name.getText().toString();
            String rate=price.getText().toString();
            mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
            mydb.execSQL("INSERT INTO add2cart (pnme,prate)VALUES('"+nm+"','"+rate+"')");
            Toast.makeText(getApplicationContext(),"add to cart",Toast.LENGTH_SHORT).show();
            Intent in=new Intent(Product_Details.this,add2cart.class);  
            startActivity(in);
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

post your SQLiteDatabase class here. Also SQlite uses Version Number you should increment this version number each time when you major change in the database file like create new table
0

You use this line mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null); to reset the mydb,now the mydb is the batabase products

2 Comments

But i need to display details of each products from products database.and on click add to cart button details should be inserted into addcart database in add2cart table.so how can i use both database in this activity
declare two Database instance。and make sure when do not use it any more,close it。

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.