Search code examples
androidsqlitecursor

Request multiple cells from SQLite database in android


I am trying to call data from two different cells in my database then combine them and print them out in an activity.

I am using the following code:

public Cursor getGermanDescription(String id) {
    String[] args = { id };
    return (getReadableDatabase()
            .rawQuery(
                    "SELECT _id,Column1,Column2 FROM Databasing_Details WHERE _id=?",
                    args));

With the above I am only getting the content of Column1 but not Column2. I am passing the String id to another activity.

My cursor adapter is:

   @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

Get our passed variable from our intent's EXTRAS

        passedVar=getIntent().getStringExtra(ListViewTwo.ID_EXTRA);

        //this is our ListView element, obtained by id from our XML layout
        ListView myListView = (ListView)findViewById(R.id.list_view);

        String string = passedVar;  
        int passedInt = Integer.parseInt(string); 

        if (passedInt==1) { passedVar1 = true;

        }

creating our database Helper:

        dbDescriptionHelper=new DatabaseHelper(this);
        //a set of results from a database query
        ourCursor=dbDescriptionHelper.getGermanDescription(passedVar);

        //tell android to start managing the cursor,
        startManagingCursor(ourCursor);
        //create our adapter
        adapter=new SlangAdapter(ourCursor);
        //set the adapter!!!
        myListView.setAdapter(adapter);

        } catch (Exception e) {

            Log.e("ERROR", "ERROR IN CODE: " + e.toString());

            e.printStackTrace();
        }

        return;
    }

The slangAdapterClass:


    class SlangAdapter extends CursorAdapter {
        SlangAdapter(Cursor c) {
          super(ListViewFinal.this, c);
        }
        @Override

        public void bindView(View row, Context ctxt,
                           Cursor c) {
            DescriptionHolder holder=(DescriptionHolder)row.getTag();
            holder.populateFrom(c, dbDescriptionHelper);
        }
        @Override
        public View newView(Context ctxt, Cursor c,
                           ViewGroup parent) {
          LayoutInflater inflater=getLayoutInflater();
          View row=inflater.inflate(R.layout.main_row, parent, false);
          DescriptionHolder holder=new DescriptionHolder(row);
          row.setTag(holder);
          return(row);
        }
      }

The DescriptionHolder class:


    static class DescriptionHolder {
        private TextView name=null;

        DescriptionHolder(View row) {
            name=(TextView)row.findViewById(R.id.row);
        }

        void populateFrom(Cursor c, DatabaseHelper r) {
            name.setText(r.getName(c));
        }
    }

Could someone point out where I am going wrong please.


Solution

  • OK, I have found the answer.

    My syntax was incorrect, what I needed to write for my cursor was:

    public Cursor getGermanDescription(String id) {
        String[] args = { id };
        return (getReadableDatabase()
                .rawQuery(
                        "SELECT _id, ObjectDescriptionGerman ||'\n\n'|| ObjectDescription  FROM Databasing_Details WHERE _id=?",
                        args));
    
    }
    

    The || needs to be used instead of "," or "AND". I have also inserted line breaks between my two returned value so I do not need to do this in my database.