有没有办法将整个SQLite查询结果存入字符串?

时间:2014-04-22 23:36:30

标签: sqlite android-sqlite sqliteopenhelper dynamicquery

我想 - 只是为了“概念证明” - 将整个查询结果集显示为字符串。我希望查询的表是一个变量。

怎么做?我有以下开始:

public String[] getAllRecordsFrom(String tblName) {
    String query = "Select * FROM " + tblName;
    String[] results = null;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db != null ? db.rawQuery(query, null) : null;
    if (cursor == null) return results;
    while (cursor.moveToFirst()) {
        results. <== now what?
    }
    cursor.close();

    if (db != null) {
        db.close();
    }
    return results;
}

...但不知道如何从“&lt; ==现在怎么办?”一部分。

更新

根据Greg的想法/代码,我将尝试这个:

// Generic * query
public StringBuilder getAllRecordsFrom(String tblName) {
    String query = "Select * FROM " + tblName;
    StringBuilder results = new StringBuilder();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db != null ? db.rawQuery(query, null) : null;
    if (cursor == null) return results;
    int colCount = cursor.getColumnCount();
    cursor.moveToFirst();
    while (true) {
        for (int i = 0; i < colCount; i++) {
            results.append(cursor.getString(i).toString());
            // if the above (getting all data types as string) doesn't work, see if this (pseudocode) will: Type t = results.append(cursor.getType(i); if t == int then getint(i), else if t == string then getString(i), &c.
        }
        if (cursor.isLast()) break;
        cursor.moveToNext();
    }
    cursor.close();

    if (db != null) {
        db.close();
    }
    return results;
}

更新2

好的,这就是这个:

private class ShowLocalDataTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... strings) {
        String tbl = strings[0];
        SQLiteOpenHelperHHS sqliteHHS = new SQLiteOpenHelperHHS(SQLiteActivity.this, null);
        StringBuilder sb = sqliteHHS.getAllRecordsFrom(tbl);
        return sb.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        EditText etmultiline = (EditText) findViewById(R.id.editTextMultiline);
        if (result == null) return;
        etmultiline.setText(result);
        Log.i("QueryResults", result);
    }
}

注意:如果“.toString()没有工作,presumably我可以使用”cursor.getType(i)“getType的返回值是: 0 == null,1 == int,2 == float(REAL),3 == String,4 == BLOb

更新3

这更加“优雅”,我认为(将列作为其类型):

public StringBuilder getAllRecordsFrom(String tblName) {
    final int NULLVAL = 0;
    final int INTVAL = 1;
    final int FLOATVAL = 2;
    final int STRINGVAL = 3;
    final int BLOBVAL = 4;

    String query = "Select * FROM " + tblName;
    StringBuilder results = new StringBuilder();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db != null ? db.rawQuery(query, null) : null;
    if (cursor == null) return results;
    int colCount = cursor.getColumnCount();
    cursor.moveToFirst();

    int typeVal;
    while (true) {
        for (int i = 0; i < colCount; i++) {
            typeVal = cursor.getType(i);
            switch (typeVal) {
                case NULLVAL:
                    // nuttin', honey
                    break;
                case INTVAL:
                    results.append(cursor.getInt(i)).toString();
                    break;
                case FLOATVAL:
                    results.append(cursor.getFloat(i)).toString();
                    break;
                case STRINGVAL:
                    results.append(cursor.getString(i));
                    break;
                case BLOBVAL:
                    //results.append(cursor.getBlob(i)).toString();
                    // Probably better off doing it this way:
                    results.append("BLOb" + String.valueOf(i));
                    break;
            }
        }
        if (cursor.isLast()) break;
        cursor.moveToNext();
    }
    cursor.close();

    if (db != null) {
        db.close();
    }
    return results;
}

1 个答案:

答案 0 :(得分:1)

你最好像这样使用StringBuilder类(注意这会给你一个巨大的字符串,而不是一个字符串数组;如果你想要一个数组,其中光标的每一行都是它自己的元素,那么将结果声明为ArrayList,将results.append替换为results.add并返回results.toArray()。

public String getAllRecordsFrom(String tblName) {
    String query = "Select * FROM " + tblName;
    StringBuilder results = new StringBuilder();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db != null ? db.rawQuery(query, null) : null;
    if (cursor == null) { return null }

    cursor.moveToFirst();
    while (true) {
        results.append(cursor.getString(0));  // this assumes only one column in the results
        if (cursor.isLast()) break;
        cursor.moveToNext();
    }
    cursor.close();

    if (db != null) {
        db.close();
    }
    return results.toString();
}