2

I have an SQLite Database in my application. It has three columns. being _id, TEXT, and Location. If I want to return all the data from, say, the TEXT column should I use cursor.getColumnIndex(2)? I am obviously new to SQLite. And and all help is appreciated. Thanks everyone!

1

3 Answers 3

6

Yes, friend, you are new.

First off, your database doesn't have three columns, but rather, your table does. Databases have tables, tables of columns (fields) and rows (records).

Secondly, TEXT is not a valid name for a column, as it's a datatype. Let's say you called the three columns id, theText, and location -- then if you selected all three columns to be returned, the second one would be accessible through:

cursor.getString(1); // that's the second column returned 

or

cursor.getString(cursor.getColumnIndex( "theText" ) ); 

However, you can have sqlite do most of the work for you by selecting only the column you're interested in, so then you'd cursor.getString(0) as it's the only column returned.

For more pertinent explanations, please post your code in the question.

Sign up to request clarification or add additional context in comments.

Comments

1

simply apply the query of getting all contacts and take an array of string type and then add the required record in that array as shown below

I hope this code help u

  1. in DBHelper getting record of particular column :

    public ArrayList<String> getAllCotactsEmail() { ArrayList<String> arrayList=new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor res = db.rawQuery( "select * from contacts", null ); res.moveToFirst(); if (res != null) { while(res.isAfterLast() == false){ arrayList.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_EMAIL))); Log.d("emailssinlisttt",arrayList.toString()); res.moveToNext(); }} return arrayList; } 
  2. retrieve :

    email=mydb.getAllCotactsEmail(); Log.d("emaillllll",email.toString()); 

Comments

0

You need to query your Database to get your data. This query will return a Cursor with the column you specified in the query.

To make query, you need to call query() method from ContentResolver. To get your ContentResolver, you can use getContentResolver() from a Context like Activity :

getContentResolver.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);

To understand all parameters, see : ContentResolver

In your case, you want only TEXT column so pass a String array with your TEXT column name for projection parameters. You want all rows so your selection and selectionArgs parameters must be null. If you don't care about order, pass null for sortOrder (rows will be sort by ID) :

Cursor c = getContentResolver.query(yourUri, new String[]{"TEXT"}, null, null, null) 

This query will return a cursor, to extract your values from the cursor, make a loop like :

if(c.moveToFirst()) { do { final String text = c.getString(c.getColumnIndex("TEXT")); } while (c.moveToNext()); } 

Hope this will help you :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.