大家好很多天过去了,我再次阅读了很多示例和线程。但我还是新手,请不要狠狠地踢我。我认为问题不是很好,但我要解释一下我在 android DB 有 3 个表。
这是三张表
- TABLE_ITEMS
- TABLE_PRIHOD(输入)
- TABLE_RASHOD(输出)商品。
我想对所有产品名称使用 id_goods,但我没有实践。
现在我可以,我必须将数据库保存在不同的两个表中,并在不同的 ListView 中获取它们。
我想首先将所有 id_goods 保存在 TABLE_ITEMS 中,然后保存到 TABLE_PRIHOD 中,因为我认为如果我拥有所有商品名称,就更容易在数据库中工作。我知道我做错了。
这里是示例
- 保存在 TABLE_PRIHOD(输入)id 1 本书 2014.02.12 500 本 2 美元。
- 在保存到 TABLE_PRIHOD 之前,我想将图书保存到 TABLE_ITEMS 以将其作为 id_goods。
- 将其用于 TABLE_PRIHOD(输入),不是将其写入书籍,而是写入 TABLE_ITEMS 中的 ID。
这是我的想象: (id)1 (id_goods)1 (日期)2014年2月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);
}
最佳答案
如果我没理解错的话,您是想先将商品插入到TABLE_ITEMS中,然后在TABLE_PRIHOD中使用插入的TABLE_ITEM记录的id吗?
如果是这样,您可以在addItemsId
中返回插入的id,因为db.insert
返回新插入行的idhttp://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String,%20java.lang.String,%20android.content.ContentValues)
这看起来就像这样
@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();
}
}
关于android - 如何保存到第二个表并在保存之前将其放入第一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746770/