1

我有一个表格和记录,例如:

EmployeeName
------------
Ram
Laxman
Bharat
Shatrugn

我想在一个查询中输出连接一行中的所有值:

我想要这样的结果:

Ram,Laxman,bharat,shatrugn

在单行中用 ,(comma) 连接字符串.. 但我不知道如何使用光标在 android 中连接...

4

3 回答 3

4

在 SQLite 中,您可以使用GROUP_CONCAT()

select Group_Concat(EmployeeName)
from table1

请参阅带有演示的 SQL Fiddle

如果您有多个要返回的字段,则可以GROUP BY在查询中使用 a,类似于:

select id, Group_Concat(EmployeeName)
from table1
group by id

请参阅带有演示的 SQL Fiddle

于 2012-10-16T10:28:29.260 回答
2
     String values;
       if (cursor.moveToFirst()) {
                do {

               values=values + cursor.getString(0)+",";

                } while (cursor.moveToNext());

删除最后一个逗号

if (values.length() > 0)
 {
    values= values.substring(0,values.length() - 1);    
  }
于 2012-10-16T10:40:04.150 回答
2

这是我使用的代码...希望对您有所帮助。

 private SQLiteDatabase myDataBase;
 Cursor cursor;
 String S="";
 String myPath2 = yourDBpath + yourDBNAME;
 try{
 myDataBase = SQLiteDatabase.openDatabase(myPath2, null,SQLiteDatabase.OPEN_READWRITE);
 String sql="your query";
 cursor=myDataBase.rawQuery(sql, null);
if(cursor != null)
{
   while(cursor.moveToNext())
   {
    S=S.append(cursor.getString(0));
    }
}
  } 
}catch(Exception e){

        }finally{
        myDataBase.close();
        }

最终结果将在 String S 中。

于 2012-10-16T11:43:45.837 回答