Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:15:31+00:00 2026-05-30T16:15:31+00:00

Hi friends. I’m trying to add a product to a SQLlite Database when a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:15:32+00:00Added an answer on May 30, 2026 at 4:15 pm

    You can try as well.

    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;
    

    }

    check if the database was created!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends, i m trying add image to my Jbutton using seticon method but it
friends i am trying to work with predesigned visual studio dotNet projects. so i
Friends, I am trying to processing a huge amount of audio files using a
friends. I know, there are many questions here already on these iterators. I've read
Friends,I am trying to calculate the difference between two date&time entries that the user
Friends I am using a list view to add particular records. I need to
friends, i want to add google like circular current location icon in my application
friends, i am trying to delete row and then when i try to fetch
Friends i am a newbie in php and trying to learn it more and
Friends, I'm lost here. I have this WCF Rest service returning data in json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.