我试图在SQLite数据库中存储数据

时间:2014-04-10 16:03:18

标签: android android-sqlite

我正在尝试修复AdapterClass有addOrder(String s, int qty)方法的问题,这些值没有插入到database.insert("CUS_MENU", null, ocv)中,问题也显示在这行中数据不是sqlite数据库中的存储。

public class AdapterClass 
{
    private Context context;
    private SQLiteDatabase database;
    CreateDB dbHelper;
    private static String DATABASE_TABLE=" ";   
    public AdapterClass(Context pcontext, String pDBTableName) 
    {
        // TODO Auto-generated constructor stub
        this.context = pcontext;
        this.DATABASE_TABLE = pDBTableName;
    }

    public AdapterClass Open() throws SQLException
    {
        dbHelper=new CreateDB(context);
        database=dbHelper.getWritableDatabase();
        return this;    
    }

    // To Insert Record
    public long insertquery(String[] pColumnNames, String[] pColumnValues) {

        ContentValues initialValues = createContentValues(pColumnNames,pColumnValues);
        return database.insert(DATABASE_TABLE, null, initialValues);

    }
    // Split the sting into two parts
    public void addOrder(String s, int qty){
        ContentValues ocv = new ContentValues();
        String a[] = s.split("-");
        String price = a[1].replaceAll("[^0-9]", "");

        int total = Integer.parseInt(price)*qty;

        ocv.put("PRICE", price);
        ocv.put("NAME", a[0]);
        ocv.put("QTY", qty);
        ocv.put("TOTAL", total);
        //db=this.getWritableDatabase();
        database.insert("CUS_MENU", null, ocv);
        database.close();   
    }

    }

这是orderscreen类错误显示在这些行order = new AdapterClass(this)order.addOrder(s,i)中。请任何人帮忙解决这个问题。

public class OrderScreen extends Activity implements OnClickListener  {
TextView orderitem;
EditText Quantity,devide_quanity;
Button Place_Order;
String tempstring;
Integer i;
String s;
String quanityforsoup;
StringBuilder orderdata=null;
AdapterClass order = null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_screen);

        orderitem =(TextView)findViewById(R.id.orderitem);
        Quantity =(EditText)findViewById(R.id.Quantity);
        Place_Order =(Button)findViewById(R.id.Place_Order_btn);

        order = new AdapterClass(this);
        Place_Order.setOnClickListener(this); 

        s= getIntent().getStringExtra("order");
        String a[] = s.split("-");
    }

    @Override
    public void onClick(View v) 
    {
        //Place_Order.setClickable(false);
        tempstring=Quantity.getText().toString();   
        i=Integer.parseInt(tempstring); 
        Toast.makeText(OrderScreen.this, s, Toast.LENGTH_LONG);
        //String  temps=s;

        //  adding the data 
        order.addOrder(s,i);
        Intent mIntent = new Intent(OrderScreen.this,MenuScreen.class);
        startActivity(mIntent);
        finish();
        orderitem.setText("You have ordered "+s);

    }

这是db Helper类:

public class CreateDB extends SQLiteOpenHelper
{
SQLiteDatabase db;
public CreateDB(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String DATABASE_NAME = "restaurant";
private static final int DATABASE_VERSION = 1;
private static final String MENU= "create table CUS_MENU(_id integer primary key autoincrement, " + "" + "NAME text,"+"QTY text,"+"PRICE text,TOTAL text);";

@Override public void onCreate(SQLiteDatabase db)
{
db.execSQL(MENU);
}
}

1 个答案:

答案 0 :(得分:0)

在click事件中写.show()来显示toast。是的错误将在那里 order = new AdapterClass(this);你写的构造函数是  public AdapterClass(Context pcontext,String pDBTableName)     {         this.context = pcontext;         this.DATABASE_TABLE = pDBTableName;     } 那么你有传递上下文和表名称。 检查你的代码甚至你写的意图转移到其他活动...你正在写完()。 检查并祝你好运