2014-12-03 72 views
0

我正在開發一個購物車應用程序。但在添加項目詳細信息到數據庫時出錯,在 點擊加入購物車 button。錯誤日誌說有沒有這樣的數據庫表。並迫使我關閉虛擬機。請幫助我。Android SQLite數據庫表 - 創建table.But錯誤日誌說沒有這樣的表

我有兩個數據庫,在此activity.problem是add2cart表addcart數據庫

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

} 

}

回答

1

我打開我的addcart數據庫在setOnClickListner和我的代碼工作。

問題是我打開addcart數據庫的地方。它只考慮Main中最後打開的數據庫。

更正代碼

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); 
     } 
    }); 
+0

在此處發佈您的SQLiteDatabase類。另外,SQlite使用'Version' Number,每次在數據庫文件中進行重大更改(如創建新表)時,都應該增加此版本號 – 2014-12-04 05:47:06

0

您可以使用此行mydb = Product_Details.this.openOrCreateDatabase("products", MODE_PRIVATE, null);重置mydb,現在是mydb是batabase products

+0

,但我需要從**產品顯示每個產品的細節上**點擊database.and添加到購物車按鈕,細節應插入** ** addcart數據庫** add2cart ** table.so如何在此活動中使用兩個數據庫 – droid 2014-12-03 06:46:00

+0

聲明兩個數據庫實例,並確保何時不再使用它,請關閉它。 – chinaanihchen 2014-12-03 08:05:25