我正在開發一個購物車應用程序。但在添加項目詳細信息到數據庫時出錯,在 點擊加入購物車 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);
}
});
}
}
在此處發佈您的SQLiteDatabase類。另外,SQlite使用'Version' Number,每次在數據庫文件中進行重大更改(如創建新表)時,都應該增加此版本號 – 2014-12-04 05:47:06