更新SQL数据库列

问题描述 投票:0回答:1

我知道已经有很多的这些问题,但遗憾的是没有这些帮助我的问题。

随着点击一个按钮,我想一些新的内容添加到我的数据库,或将现有列应被覆盖。添加内容到我的数据库能正常工作。但是如果我想覆盖现有的内容,我的代码只是增加了一个新行到数据库。

这是我的代码:

主要活动:添加内容巴顿为BD

btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListView shopList= (ListView) findViewById(R.id.lvShopList);


            for(int i = 0; i < dataSource.size();i++){
                tvname = (TextView) shoppingList.getChildAt(i).findViewById(R.id.tvName);
                tvamount= (TextView) shoppingList.getChildAt(i).findViewById(R.id.tvAmount);

                String nameTV = tvname.getText().toString();
                String amaountTV = tvamount.getText().toString();

                ingredient.setName(nameTV);
                ingredient.setAmpunt(amaountTV );
                myDB.getInstance(MainActivity.this).increaseIngredient(ingredient);
            }
        }
    });

这是我的DB类

public Ingredient increaseIngredient(final Ingredient ingredient){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(COL_NAME, ingredient.getName());
    values.put(COL_AMOUNT, ingredient.getAmount());

    long newID = db.insert(TABLE_NAME, null, values);
    db.close();
    return getiIngredient(newID);

    //Also tried this code, but with this, nothing will be shown:
    // db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[]{String.valueOf(ingredient.getId())});
    // db.close();
    // return getiIngredient(ingredient.getId());
}

public Ingredient getIngredient(final long id){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.query
            (TABLE_NAME, new String[]{KEY_ID, COL_NAME, COL_AMOUNT},
            KEY_ID + " = ?",
            new String[]{String.valueOf(id)},
            null, null, null);

    Ingredient ingredient = null;

    if(c != null && c.getCount() > 0) {
        c.moveToFirst();
        ingredient = new Ingredient(c.getString(c.getColumnIndex(COL_NAME)));
        ingredient.setId(c.getLong(c.getColumnIndex(KEY_ID)));
        ingredient.setAmount(c.getString(c.getColumnIndex(COL_AMOUNT)));
    }
    db.close();
    return ingredient;
}

这是我的模型类

public class Ingredient implements Serializable {
    private long id;
    private String name;
    private String amount;

    public Zutaten() {
        this(null, null);
    }

    public Zutaten(String name) {
        this(name, null);
    }

    public Zutaten(String name, String amount) {
        this.name = name;
        this.amount= amount;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount= amount;
    }
}

编辑:这是我的活动,我想展现DB内容:

public class StorageDB extends AppCompatActivity {
    myDB dbIngredients;

    ListView lvDB;
    TextView name, amount;

    ArrayList<Ingredients> dataSource;
    DbStorageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ingredient_storage);

        lvDB= (ListView) findViewById(R.id.lvDB);
        dataSource = dbIngredients.getInstance(this).getAllIngredient();

        this.adapter = new DbStorageAdapter (dataSource, this);
        lvDB.setAdapter(adapter);

        name = (TextView)findViewById(R.id.tvName);
        amount= (TextView)findViewById(R.id.tvAmount);

        showDBcontent();
    }

     private void showDBcontent(){
        myDB db = new myDB (this);
        ArrayList<Ingredients> iList = db.getAllIngredient();
        adapter = new DbStorageAdapter (zList, this);
        lvDB.setAdapter(adapter);
     }
}

所以,如果我使用更新,在我的存储活动列表视图是空的,如果我使用插入所有从数据库中的行会被显示。

随着第一次点击我8行添加到数据库中。如果我使用db.insert,然后点击按钮,我有16排在我的数据库,所有16列,将在存储活动显示。

第二个编辑:

其实我只是想,如果表中存在按钮后,我的代码检查点击。如果它存在,我想这将更新我的表行。如果表不存在,我想它插入我的内容通过点击按钮发送。

我想这一点,但它不工作:

public Ingredient increaseIngredient (final Ingredient ingredient ){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery
            ("SELECT * FROM " + TABLE_NAME + " WHERE " + COL_NAME + " = ?",
                    null);
    ContentValues values = new ContentValues();

    values.put(COL_NAME, ingredient .getName());
    values.put(COL_AMOUNT, ingredient .getAmount());

   if(c !=null && c.getCount() > 0){
        db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[]{String.valueOf(zutaten.getId())});
        db.close();
        return getZutat(zutaten.getId());
    } else {
        long newID = db.insert(TABLE_NAME, null, values);
        db.close();
        return getZutat(newID);
    }
java android database sql-update
1个回答
1
投票

这个:

db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[]{String.valueOf(ingredient.getId())});

将更新id = ingredient.getId()行如果ID存在于表。 你打电话时:

myDB.getInstance(MainActivity.this).increaseIngredient(ingredient);

ingredient对象不包含要更新id。 如果你做这样的事情:

ingredient.setId(10);

在调用increaseIngredient()然后,如果存在与ID = 10的行,则该行被更新。 从评论编辑: 我的意思是这样的:

public Ingredient increaseIngredient (Ingredient ingredient) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL_NAME, ingredient.getName());
    values.put(COL_AMOUNT, ingredient.getAmount());

    if (ingredient.getId() == null) {
        long newID = db.insert(TABLE_NAME, null, values);
        db.close();
        return ???;
    } else {
        db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[]{String.valueOf(ingredient.getId())});
        db.close();
        return ???;
    }
}

问号的意思是我不知道你需要什么回报。

© www.soinside.com 2019 - 2024. All rights reserved.