enter image description here我正在尝试更新(如果可能)或在android中插入我的数据库。我已经实现了我的代码,但它没有按照我的意愿工作。因为我是android新手,可能是我的代码中的错误。所以在这个场景中,是否有一些具有一定数量的项目,现在当我点击"添加到购物车"按钮这个项目应该插入我的数据库,现在当项目的数量被更改,然后我想只更新我的数据库。但我的代码总是在数据库的新行中插入一个项目。这是我的代码片段
addcart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Integer.parseInt(itemquant.getText().toString())>0){
SQLiteDatabase db=cDBHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(CartContract.CartEntry.CART_NAME,t1.getText().toString());
values.put(CartContract.CartEntry.CART_PRICE,Integer.parseInt(t2.getText().toString()));
values.put(CartContract.CartEntry.CART_QUANTITY,Integer.parseInt(itemquant.getText().toString()));
int id=(int)db.insertWithOnConflict(CartContract.CartEntry.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_IGNORE);
if(id==-1)
{
db.update(CartContract.CartEntry.TABLE_NAME,values, CartContract.CartEntry.CART_NAME+"=?",new String[] {t1.getText().toString()});
}
Toast.makeText(MainActivity.this,String.valueOf(id),Toast.LENGTH_LONG).show();}
}
});
答案 0 :(得分:0)
您应该使用 DatabaseHelper 类来处理所有这些逻辑。
以下是DatabaseHelper类的简单示例。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "currencies.db";
private static final String TABLE_NAME = "currency_table";
private static final String TABLE2_NAME = "currency_list_table";
private static final String COL_1 = "ID";
private static final String COL_2 = "SYMBOL";
private static final String COL_3 = "CURRENCY";
private static final String COL_4 = "BALANCE";
private static final String COL_5 = "PRICEPER";
private static final String COL_6 = "IMAGEURL";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, SYMBOL TEXT, CURRENCY TEXT, BALANCE REAL, PRICEPER REAL, IMAGEURL TEXT)" );
db.execSQL("create table " + TABLE2_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, SYMBOL TEXT, CURRENCY TEXT, IMAGEURL TEXT)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME );
db.execSQL("DROP TABLE IF EXISTS " + TABLE2_NAME );
onCreate(db);
}
public void insertData(boolean isFirstTable, String symbol, String currency, double balance, double pricePer, String imageUrl) {
SQLiteDatabase db = this.getWritableDatabase();
/* Creating content values for columns */
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, symbol);
contentValues.put(COL_3, currency);
contentValues.put(COL_6, imageUrl);
if (isFirstTable) {
contentValues.put(COL_4, balance);
contentValues.put(COL_5, pricePer);
/* Inserting data into DB */
db.insert(TABLE_NAME, null, contentValues);
}
else
db.insert(TABLE2_NAME, null, contentValues);
}
public void deleteData(boolean isFirstTable, String symbol) {
SQLiteDatabase db = this.getWritableDatabase();
/* Deletes data by ID */
if(isFirstTable)
db.delete(TABLE_NAME, "SYMBOL = ?", new String[] { symbol } );
else
db.delete(TABLE2_NAME, "SYMBOL = ?", new String[] { symbol } );
}
public Cursor getAllData(String table) {
SQLiteDatabase db = this.getWritableDatabase();
/* Returns all data */
return db.rawQuery("select * from " + table, null);
}
}
然后将数据插入数据库将非常简单
/* Insertion into DB */
myDB.insertData(true, symbol, chosenCoin, chosenAmount, pricePer, imageUrl);
要访问数据库, myDB
myDB = new DatabaseHelper(this);
Cursor res = getDatabase("currency_table");