查询语法确认

时间:2015-11-04 11:41:33

标签: android sqlite

从表中获取特定列是否正确?表名 - FI_GOODS_DETAIL_TABLE,列名 - FIGOODSDETAIL_FINISHGOODS ......

“选择”+ FIGOODSDETAIL_FINISHGOODS +“from”+ FI_GOODS_DETAIL_TABLE +“;”;

我将列值存储在arrray中

public List<String> getAllLabels(){
            List<String> labels = new ArrayList<String>();

            // Select All Query
            String selectQuery = "select " + FIGOODSDETAIL_FINISHGOODS + "from " + FI_GOODS_DETAIL_TABLE + ";";
            try {
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            } catch (Exception exe) {
                exe.printStackTrace();
                db.endTransaction();
                Log.e("Insertion failed",
                        "Transaction failure when inserting login data.");
                this.closeDatabase();
                Log.i("DB closed", "Database closed successfully.");
                errCode = "Err-DB-06";
                LogFileCreator.appendLog(errCode + " : " + exe.getMessage());
            }

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    labels.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }

            // closing connection
            cursor.close();
            db.close();

            // returning lables
            return labels;
        }

1 个答案:

答案 0 :(得分:1)

试试这样:

    public List<String> getAllLabels() throws SQLiteException {
        List<String> labels = new ArrayList<String>();

        // Select All Query
        String selectQuery = "select " + FIGOODSDETAIL_FINISHGOODS + " from " + FI_GOODS_DETAIL_TABLE ;

        SQLiteDatabase db = this.getReadableDatabase();

        try {

          Cursor cursor = db.rawQuery(selectQuery, null);

          // looping through all rows and adding to list
          if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(1));
            } while (cursor.moveToNext());
          }

          cursor.close();

        } catch (RuntimeException exe) {

            exe.printStackTrace();

            this.closeDatabase(); // What does this do that db.close() does not?
            errCode = "Err-DB-06";
            LogFileCreator.appendLog(errCode + " : " + exe.getMessage());

            throw exe;

        } finally {
          db.close();
        }

        // returning lables
        return labels;
    }