【问题标题】:Increment a counter in database SQLite在数据库 SQLite 中增加一个计数器
【发布时间】:2015-10-30 09:20:37
【问题描述】:

我正在做一个关于 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; }

【问题讨论】:

  • 你的意思是更新KEY_COUNT + 1 吗?
  • 嗨@calvinfly,我已经对代码进行了编辑,但是当我点击按钮时应用程序仍然崩溃

标签: java android sqlite estimote


【解决方案1】:

若要将 counter 中的值加一,您需要向 Table 发起 UPDATE 查询。

如下:

UPDATE "Your tablename"
SET "counter column name"= "counter column name"+ 1
WHERE id= "pass your reference id";

希望对你有所帮助。

【讨论】:

  • 您好,我添加了以下代码,请您看看是否有什么需要改进或更改的地方
  • 字符串 countQuery = "UPDATE " + TABLE_SRESULT + " SET " + KEY_COUNT + " = " + KEY_COUNT + "+1" + " WHERE " + KEY_IDENTIFIER + "= " + identifier + "'"; Log.e("countQuery",countQuery)
  • 添加这些代码还是有错误
  • countQuery 是什么?
  • 您好,我已经解决了问题,我会更新解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 2011-04-02
相关资源
最近更新 更多