Android Sqlite 合并两列

3
我已经完成了连接操作,但结果显示为


UncleSam.

我想做的是在这两列之间加上一个空格,结果应为:
Uncle Sam.

这是我正在处理的代码:

   public Cursor getAllPatients()
{
    Cursor localCursor = //  
            this.myDataBase.query(DB_TABLE, new String[] { 
                    KEY_ID, KEY_FNAME + "||" + KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP }, null, null, null, null, null);
    if (localCursor != null)
      localCursor.moveToFirst();
    return localCursor;
}

来自 DBHelper.java

以及

 Cursor cursor = dbHelper.getAllPatients();
    String[] data = new String[]{
            DBHelper.KEY_FNAME + "||" + DBHelper.KEY_LNAME, DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP};
    // 
    int[] to = new int[] {R.id.fullname, R.id.diagnosis, R.id.lastffup};

    dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_row, cursor, data, to, 0);

来自我的MainActivity。

任何帮助都可以。谢谢。


在检索名字和姓氏时,是否需要在它们之间添加一个额外的空格? - ClaireG
当我尝试那样做时,它抛出了一些错误。 - Dunkey
你需要将它们连接起来做什么? - Simon Dorociak
@Sajmon需要显示为一个,但我需要在它们之间加上一个空格。 - Dunkey
你想要像“山姆大叔”一样从光标中打印数据吗?其中“大叔”是来自一列的值,而“山姆”则来自另一列。 - Simon Dorociak
是的,就像名字和姓氏一样。 - Dunkey
3个回答

3
你可以像这样链接字符串:
Cursor localCursor = this.myDataBase.rawQuery("SELECT (KEY_FNAME  || ' ' || KEY_LNAME) AS fullname, KEY_ID, KEY_DIAGNOSIS, KEY_LASTFFUP FROM DB_TABLE");

你的合并全名将出现在光标列“fullname”中。

在主活动中:

String[] data = new String[]{ "fullname", DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP};

你可能需要为“fullname”分配一个DBHelper常量。

但是我的MainActivity呢? - Dunkey
简单:String[] data = new String[]{ "fullname", DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP}; - VM4
1
我认为它应该是这样的 SELECT KEY_FNAME || ' ' || KEY_LNAME as fullname, KEY_ID, KEY_DIAGNOSIS, KEY_LASTFFUP FROM DB_TABLE。您可以使用 PipeSQLite 中连接两个列。 - user456118
是的,你可能是对的,不确定这是否适用于SQLite。我会进行编辑。 - VM4

0
Android Sqlite连接两个列
在与本主题作者的一番交谈后,我建议您不要连接列(您确实不需要它),而是连接从Cursor检索到的字符串。这样,您的语句将更易读,并且解决方案更加清晰。
解释:
通常,使用对象来表示表格是非常有用和高效的方法。例如,如果您有一个名为User的表格,则创建一个新类User,表格中的列将等于此类中的属性。这种方式相当优雅(如果其他人看到您的代码,他们不会感到困惑和害怕)。
最后,当您将它们添加到ListAdapter时,您可以简单地连接fname和lname。

所以我更喜欢这种方式:

List<User> users = new ArrayList<User>();
User u = null;
String query = "select * from Table";
Cursor c = db.rawQuery(query, null);
if (c != null && c.moveToFirst()) {
   do { 
      u = new User();
      u.setFirstName(c.getString(c.getColumnIndex("fname")));
      u.setLastName(c.getString(c.getColumnIndex("lname")));
      ...
      users.add(u);
   } while (c.moveToNext());
}

然后在ListAdapter的某个地方,您可以执行以下操作:

textView.setText(u.getFirstname() + " " + u.getLastName());

希望这有所帮助。

我有点不同意这个解决方案更加清晰的说法。精确选择你需要的比不必要地循环遍历数据要干净得多。SimpleCursorAdapter的目的就在于避免这些循环。 - VM4
@VM 这只是理论。在实际应用中,通常采用这种方法。 - Simon Dorociak

0

我终于让它工作了。

 public Cursor getAllPatients()
 {
Cursor localCursor = //  
        this.myDataBase.query(DB_TABLE, new String[] { 
                KEY_ID, KEY_FNAME + "|| ' ' ||" + KEY_LNAME, KEY_DIAGNOSIS, KEY_LASTFFUP }, null, null, null, null, null);
if (localCursor != null)
  localCursor.moveToFirst();
return localCursor;
}

Cursor cursor = dbHelper.getAllPatients();
String[] data = new String[]{
        DBHelper.KEY_FNAME + "|| ' ' ||" + DBHelper.KEY_LNAME, DBHelper.KEY_DIAGNOSIS, DBHelper.KEY_LASTFFUP};
// 
int[] to = new int[] {R.id.fullname, R.id.diagnosis, R.id.lastffup};

dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_row, cursor, data, to, 0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接