SQLite dB Android永远不会更新

时间:2012-03-01 12:41:26

标签: android sql database

嗨朋友们。我正在尝试在选中checkBox时将产品添加到SQLlite数据库。 这是我的SQL类:

package com.pizzeria.uno;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;



public class SQLManager  {

public static final String KEY_ROWID = "_id";
public static final String KEY_PRODUCT = "product";
public static final String KEY_QUANTITY = "quantity";
public static final String KEY_PRICE = "price";

private static final String DATABASE_NAME = "PedidoDb";
private static final String DATABASE_TABLE = "PedidoTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null,  DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                KEY_PRODUCT + " TEXT NOT NULL, " +
                KEY_QUANTITY + " TEXT NOT NULL, " +
                KEY_PRICE + " TEXT NOT NULL);"

        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);

    }


}

public SQLManager(Context c){

    ourContext = c;

}

public SQLManager open() throws SQLException{

    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

   public void close(){

    ourHelper.close();

}

    public long createEntry(String product, String quantity, String price) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_PRODUCT, product);
cv.put(KEY_QUANTITY, quantity);
cv.put(KEY_PRICE, price);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_PRODUCT, KEY_QUANTITY, KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null,     null);
String result = "";

int iRow =  c.getColumnIndex(KEY_ROWID);
int iProduct =  c.getColumnIndex(KEY_PRODUCT);
int iQuantity =  c.getColumnIndex(KEY_QUANTITY);
int iPrice =  c.getColumnIndex(KEY_PRICE);

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    result = result + c.getString(iRow) + " " + c.getString(iProduct) + " " + c.getString(iQuantity) + " " + c.getString(iPrice) + "\n";
}

return result;
    }

   }

以下是我尝试将数据添加到数据库的地方:

            package com.pizzeria.uno;

            import android.app.Activity;
            import android.app.Dialog;
            import android.content.Intent;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.CheckBox;
            import android.widget.CompoundButton;
            import android.widget.CompoundButton.OnCheckedChangeListener;
            import android.widget.TextView;

            import com.pizzeriabritannia.com.R;

    public class Vindaloo extends Activity  {


CheckBox cb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vindaloo);


final CheckBox cb = (CheckBox)findViewById(R.id.checkBox1);
cb.setText("Añadelo a mi pedido");
final Dialog d = new Dialog(this);
final TextView tv = new TextView(this);

cb.setOnCheckedChangeListener(
        new CheckBox.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                                                      boolean isChecked) {
                if (isChecked) {
                     {
                        boolean didItWork = true;
                        try{
                            String product = "Pork Vindaloo";
                            String quantity = "1";
                            String price = "25";

                            SQLManager entry = new SQLManager(Vindaloo.this);
                            entry.open();
                            entry.createEntry(product, quantity, price);
                            entry.close();

                        }catch (Exception e){
                            didItWork = false;
                            String error = e.toString();
                            d.setTitle("Fuck");
                            tv.setText(error);
                            d.setContentView(tv);
                            d.show();

                        }finally{
                            if (didItWork){

                        d.setTitle("Producto Añadido");
                        tv.setText("Success");
                        d.setContentView(tv);
                        d.show();
                            }
                        }
                    }
                }
                else {
                    cb.setText("Checkbox desmarcado!");
            }
        }
    });



    // TODO Auto-generated method stub

}

}

最后用于查看de DATABASE的活动:

       package com.pizzeria.uno;

       import android.app.Activity;
       import android.os.Bundle;
       import android.widget.TextView;
       import com.pizzeriabritannia.com.R;

       public class Visual extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.visual);

    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    SQLManager info = new SQLManager(this);
    info.open();
    String data= info.getData();
    info.close();
    tv.setText(data);

    }
}

问题是,当我尝试查看数据库时,没有添加任何内容。这很奇怪,因为当我检查de checkBox时,我可以看到带有“成功”文本的对话框,好像它已经完成了。 任何的想法??谢谢!

1 个答案:

答案 0 :(得分:1)

您也可以尝试。

public Integer createEntry(String product, String quantity, String price) {

ContentValues cv = new ContentValues();
cv.put(KEY_PRODUCT, product);
cv.put(KEY_QUANTITY, quantity);
cv.put(KEY_PRICE, price);

Integer id = (int) ourDatabase.insert(DATABASE_TABLE, null, cv)
return id;

}

检查数据库是否已创建!

相关问题