Android SQLite数据库表 - 创建表。但错误日志说没有这样的表

时间:2014-12-03 05:06:04

标签: android database sqlite

我正在开发一个购物车应用程序。但在将项目详细信息添加到数据库时出错了 点击添加到购物车按钮。错误日志显示没有此类数据库表。并强制我关闭虚拟机。请帮帮我。

我在此活动中有两个数据库。问题是 addcart数据库中的 add2cart表

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 个答案:

答案 0 :(得分:1)

我在setOnClickListner中打开了 addcart 数据库,我的代码工作正常。

问题是我打开 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);
        }
    });

答案 1 :(得分:0)

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