我正在做一个关于estimote鞋贴的项目,我现在正在为贴纸做数据库。我现在的问题是如何在数据库sqlite中设置计数器。每当贴纸被拾取或点击时,在数据库中它将显示计数的增量。我在下面发布了数据库和主要活动的代码。
NearablesDemoActivity.java
private void displayCurrentNearableInfo() {
stickerdb = new Database_sticker(this);
dbRow = stickerdb.getResult(currentNearable.identifier);
dbRow.getId();
dbRow.getIdentifier();
count = stickerdb.getCount(currentNearable.identifier);
dbRow.getCount();
String desc = dbRow.getDesc().toString();
dbRow.getCa().toString();
dbRow.getSa().toString();
String coo = dbRow.getCoo().toString();
String sm = dbRow.getSm().toString();
String price = dbRow.getPrice().toString();
//Set the text to the TextView
Desc.setText(desc);
COO.setText(coo);
SM.setText(sm);
Price.setText("$" + price);
}
Database_sticker.java
public int getCount(String identifier) {
String countQuery = "UPDATE" + TABLE_SRESULT + " SET " + KEY_COUNT + "=" + KEY_COUNT + "+1"
+ " WHERE " + KEY_IDENTIFIER + "='" + identifier + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
if(cursor != null && !cursor.isClosed()){
cursor.close();
}
return cursor.getCount();
}
public Sresult getResult(String identifier) {
String selectQuery = "SELECT * FROM " + TABLE_SRESULT + " WHERE " + KEY_IDENTIFIER + "='" + identifier + "'";
SQLiteDatabase db = this.getWritableDatabase(); //open database
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all rows and adding to list
Sresult sresult = new Sresult();
if (cursor.moveToFirst()) {
do {
sresult.setId(Integer.parseInt(cursor.getString(0)));
sresult.setIdentifier(cursor.getString(1));
sresult.setDesc(cursor.getString(2));
sresult.setSa(cursor.getString(3));
sresult.setCa(cursor.getString(4));
sresult.setCoo(cursor.getString(5));
sresult.setSm(cursor.getString(6));
sresult.setPrice(Float.parseFloat(cursor.getString(7)));
sresult.setCount(Integer.parseInt(cursor.getString(8)));
} while (cursor.moveToNext());
}
return sresult;
}
ListNearablesActivity.java
beaconManager.setNearableListener(new BeaconManager.NearableListener() {
@Override
public void onNearablesDiscovered(List<Nearable> nearables) {
toolbar.setSubtitle("Found shoes: " + nearables.size());
adapter.replaceWith(nearables);
for (Nearable nearable : nearables) {
if (nearable.isMoving) {
try {
Class<?> clazz = Class.forName(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY));
Intent intent = new Intent(ListNearablesActivity.this, clazz);
intent.putExtra(EXTRAS_NEARABLE, adapter.getItem(nearables.indexOf(nearable)));
startActivity(intent);
} //close for try
catch (ClassNotFoundException e) {
Log.e(TAG, "Finding class by name failed", e);
} //close for catch (ClassNotFoundException e)
}
}
} //for override
}); //for beaconManager.setNearable
private AdapterView.OnItemClickListener createOnItemClickListener() {
return new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
if (getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY) != null){
try {
Class<?> clazz = Class.forName(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY));
Intent intent = new Intent(ListNearablesActivity.this, clazz);
intent.putExtra(EXTRAS_NEARABLE, adapter.getItem(position));
startActivity(intent);
} //close for try
catch (ClassNotFoundException e) {
Log.e(TAG, "Finding class by name failed", e);
} //close for catch (ClassNotFoundException e)
} //close for getintent.getStringExtra()
} //close for public void onitemclick
}; //close for return new adapterview
} //close for private adapter
解决方案
Database_sticker.java
public int getStickerCount(String identifier) {
String countQuery = "UPDATE " + TABLE_SRESULT + " SET " + KEY_SCOUNT + " = " + KEY_SCOUNT + "+1" + " WHERE " + KEY_IDENTIFIER + "= '" + identifier + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int scount = 0;
if (cursor.moveToFirst())
{
do
{
scount = Integer.parseInt(cursor.getString(8));
} while(cursor.moveToNext());
}
return scount; }
答案 0 :(得分:1)
要在计数器中将值递增1,您需要触发UPDATE查询到表。
如下所示:
UPDATE "Your tablename"
SET "counter column name"= "counter column name"+ 1
WHERE id= "pass your reference id";
希望它会对你有所帮助。