如何保存到第二个表并保存之前将其放到第一个表中

时间:2014-02-13 06:25:18

标签: android mysql android-sqlite

大家好,很多天过去了,我又读了很多例子和帖子。但我又是新手请不要踢我。我认为问题不是很好但我要解释我在android DB上有3个表 这是三个表

  1. TABLE_ITEMS
  2. TABLE_PRIHOD(它的输入)
  3. 商品的TABLE_RASHOD(它的输出)。
  4. 我想将id_goods用于所有产品名称,但我没有练习。

    现在我可以,我必须在不同的2个表中保存数据库并将它们带到不同的 listview

    我想先将所有id_goods保存在TABLE_ITEMS中,然后保存到TABLE_PRIHOD,因为我认为我的所有商品名称都更容易在DB上工作。我知道我做错了。

    HERE是示例

    • 保存在TABLE_PRIHOD(输入)id 1 books 2014.02.12 500 pieces 2 $。
    • 在保存到TABLE_PRIHOD之前,我想将书籍保存到TABLE_ITEMS以将其作为id_goods。
    • 将它用于TABLE_PRIHOD(输入)不写为书籍,而是从TABLE_ITEMS写入其ID。

    我的想象: (id)1(id_goods)1(日期)2014.02.12。 (金额)500(成​​本)2

    My full CODE is here in this LINK pastie

    public class DBHandlerImpl extends SQLiteOpenHelper implements  DBHandler<Goods>{
         // all DB-variabls here
    
    
    public DBHandlerImpl(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "create table " + TABLE_ITEMS + "(" + ID + " integer primary key autoincrement, " + ID_GOODS + " integer);";
        db.execSQL(query);
    
         query = "create table " + TABLE_PRIHOD + "(" + ID_PRIHOD + " integer primary key autoincrement, " +
                                                         GOODS + " TEXT, " +
                                                         DATE + " TEXT, " +
                                                         AMOUNT + " INTEGER, " +
                                                         COST + " INTEGER);";
        db.execSQL(query);
    
        query = "create table " + TABLE_RASHOD + "(" + ID_PRODANO + " integer primary key autoincrement, " +
                                                        PRODANO + " TEXT, " +
                                                        PRODANO_DATE + " TEXT, " +
                                                        PRODANO_AMOUNT + " INTEGER, " +
                                                        PRODANO_COST + " INTEGER);";
        db.execSQL(query);
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRIHOD);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RASHOD);
        onCreate(db);
    }
    
    @Override
    public void addItemsId(Goods goods) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(GOODS, goods.getName_goods());
        db.insert(TABLE_ITEMS, null, values);
        db.close();
    }
    
    @Override
    public void addPrihod(Goods goods) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
     //   values.put(ID, goods.getId());
        values.put(GOODS, goods.getName_goods());
        values.put(DATE, goods.getDate());
        values.put(AMOUNT, goods.getAmount());
        values.put(COST, goods.getCost());
        db.insert(TABLE_PRIHOD, null, values);
        db.close();
    }
    
    @Override
    public void addRashod(Goods goods) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(PRODANO, goods.getName_goods());
        values.put(PRODANO_DATE, goods.getDate());
        values.put(PRODANO_AMOUNT, goods.getAmount());
        values.put(PRODANO_COST, goods.getCost());
        db.insert(TABLE_RASHOD, null, values);
        db.close();
    }
    public class Prihod extends Activity{
         //all variables are here
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prihod);
        //initialize code here 
        //findViewById also located here
    
    
    }
    
    public void saveTovar(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(Prihod.this);
        builder.setTitle("Потдвердить сохранение...");
        builder.setMessage("Вы уверены, что хотите сохранить?");
        builder.setIcon(R.drawable.save);
        builder.setPositiveButton("Да", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               goods = new Goods(1,edtTovar.getText().toString(),
                                    editDate.getText().toString(),
                                    editAmount.getText().toString(),
                                    editCost.getText().toString());
                db.addPrihod(goods);
                Toast.makeText(getApplicationContext(), "Новый товар сохранен!", Toast.LENGTH_SHORT).show();
            }
        });
    
        builder.setNegativeButton("Нет", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    
        builder.show();
    }`  `public class Rashod extends Activity{
    private DBHandler<Goods> db;
    private Goods goods;
    private EditText editRashod;
    private EditText editDateRashod;
    private EditText editAmountRashod;
    private EditText editCostRashod;
    private Button btn_rashod_save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rashod);
        TextView textTovar = (TextView)findViewById(R.id.textTovar);
        TextView textDate = (TextView)findViewById(R.id.textDate);
        TextView textAmount = (TextView)findViewById(R.id.textAmount);
        TextView textCost = (TextView)findViewById(R.id.textCost);
        editRashod = (EditText)findViewById(R.id.editRashod);
        editDateRashod = (EditText)findViewById(R.id.editDateRashod);
        editAmountRashod = (EditText)findViewById(R.id.editAmountRashod);
        editCostRashod = (EditText)findViewById(R.id.editCostRashod);
        btn_rashod_save = (Button)findViewById(R.id.btn_rashod_save);
        db = new DBHandlerImpl(this);
    }
    

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想先将商品插入TABLE_ITEMS,然后使用TABLE_PRIHOD中插入的TABLE_ITEM记录的id?

如果是这样,您可以在addItemsId中返回插入的ID,因为db.insert会返回新插入的行http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String,%20java.lang.String,%20android.content.ContentValues)的ID

这看起来像这样

@Override
public long addItemsId(Goods goods) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(GOODS, goods.getName_goods());
    long goods_id = db.insert(TABLE_ITEMS, null, values);
    db.close();

    return goods_id;
}

@Override
public void addPrihod(Goods goods) {

    long goods_id = addItemsId(goods);
    // -1 means an error occured
    if(goods_id != -1){

        goods.setId(goods_id);
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(ID_GOODS, goods_id);
        values.put(GOODS, goods.getName_goods());
        values.put(DATE, goods.getDate());
        values.put(AMOUNT, goods.getAmount());
        values.put(COST, goods.getCost());
        db.insert(TABLE_PRIHOD, null, values);
        db.close();
     }
}