Hi friends. I’m trying to add a product to a SQLlite Database when a checkBox is checked.
Here is my SQL class:
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;
}
}
And here is where i try to add the data to the database:
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
}
}
And finally the activity which is used to view 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);
}
}
The problem is that when i try to view the Database, nothing has been added. It’s quite strange because when i check de checkBox, i can see the dialog box with the text “success” as if it had been done well.
Any idea?? Thank you!
You can try as well.
}
check if the database was created!