Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Sqlite concat two columns

Tags:

android

sqlite

I have made the concatenation so far, but in the results, it displays

UncleSam.

What I want to do is to put a space between the two columns so the result would be

Uncle Sam.

This is the code I'm working on:

   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;
}

from DBHelper.java

and

 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);

from my MainActivity.

Any help will do. Thanks.

like image 813
Dunkey Avatar asked Feb 06 '26 16:02

Dunkey


1 Answers

You can concat like this:

Cursor localCursor = this.myDataBase.rawQuery("SELECT (KEY_FNAME  || ' ' || KEY_LNAME) AS fullname, KEY_ID, KEY_DIAGNOSIS, KEY_LASTFFUP FROM DB_TABLE");

Your concated full name will be in the cursor column 'fullname'.

In main activity:

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

(You should probably assign a DBHelper constant for "fullname").

like image 68
VM4 Avatar answered Feb 09 '26 10:02

VM4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!