[英]Concat database tables 1 field multiple records string in one row
I have a tables and records like : 我有一个表和记录,如:
EmployeeName
------------
Ram
Laxman
Bharat
Shatrugn
where i want to output to concat all values in one row in a single query: 我想在单个查询中输出以合并所有值的地方:
I want result like: 我想要这样的结果:
Ram,Laxman,bharat,shatrugn
Concat string with ,(comma) in singlee line.. but i don't know that how to concat in android using cursor... Concat字符串,在单行中带有(,逗号)。但是我不知道如何使用光标在Android中进行concat ...
In SQLite, you can use GROUP_CONCAT()
: 在SQLite中,可以使用
GROUP_CONCAT()
:
select Group_Concat(EmployeeName)
from table1
See SQL Fiddle with Demo 参见带有演示的SQL Fiddle
If you had multiple fields that you want to return, then you would use a GROUP BY
with the query, similar to this: 如果您要返回多个字段,则可以在查询中使用
GROUP BY
,如下所示:
select id, Group_Concat(EmployeeName)
from table1
group by id
String values;
if (cursor.moveToFirst()) {
do {
values=values + cursor.getString(0)+",";
} while (cursor.moveToNext());
remove last comma 删除最后一个逗号
if (values.length() > 0)
{
values= values.substring(0,values.length() - 1);
}
Here is my code i used...hope it helps you. 这是我使用的代码...希望对您有所帮助。
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();
}
Final result will be there in String S. 最终结果将在字符串S中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.