在android中获取Sqlite中的所有列值

时间:2013-04-01 08:34:32

标签: android android-sqlite

我已将产品名称保存在Sqllite数据库的一列中。现在已经保存了很多产品名称,我需要将所有产品名称发送到Web服务。例如egg,briyani,idly。 我在Sqllite中使用了这段代码但是String没有附加。我在下面提到了这段代码:

public String fetchMyRowid(String column_name)
    {
        String query = "select  "+column_name+" From " + TABLErestaurant;

        mCursor =db.rawQuery(query, null);
        StringBuffer buf = new StringBuffer();
        if (mCursor.moveToFirst()) {

            buf.append(mCursor.getString(0)+",");

        }

       return buf.toString();
     }

3 个答案:

答案 0 :(得分:2)

if (mCursor.moveToFirst()) {
 buf.append(mCursor.getString(0)+",");
}

问题是你没有迭代你的结果。这样做:

while(mCursor.moveToNext()) {

 buf.append(mCursor.getString(mCursor.getColumnIndex(column_name))+",");

}

答案 1 :(得分:0)

在这里做一个小改动,

而不是这一行,

String query = "select  "+column_name+" From " + TABLErestaurant;

试试这一行

String query = "select  * From " + TABLErestaurant;

答案 2 :(得分:0)

您的代码没问题,但是你要封锁:db.getWritableDatabase();getreadableDatabase()

我在此处修改了您的代码DBController是我的班级public class DBController extends SQLiteOpenHelper

所以你的代码将是..

public String fetchMyRowid(String column_name, DBController db) {
        String query = "SELECT  " + column_name + " FROM " + TABLErestaurant;
        SQLiteDatabase dd = db.getWritableDatabase();
        cursor = dd.rawQuery(query, null);
        StringBuffer buf = new StringBuffer();
        if(cursor.getCount() > 0 || cursor != null){

             do {

              Log.e("name--->", "" + cursor.getString(0));
              buf.append(cursor.getString(0) + ",");

              } while (cursor.moveToNext());

              Log.e("name---> buffeer-->", "" + buf);

          }

        return buf.toString();
    }