I have a database in my app, which I fill with data and an ID.
Then I request the database with the ID's I want:
Cursor cursor = contentResolver.query(uriPath, null, "ID = ?", new String[]{id1, id2, etc.}, null);
This retrieves me a cursor with all requested ID's .
How can I retrieve all objects in my UriPath ?
Just enter the Uri and leave the rest null
Cursor cursor = contentResolver.query(uriPath, null, null, null, null);
Related
Here is the code, by this I can retrieve all the columns data from the database. But the problem is that now I want to retrieve only one column, that is my requirement.link1st link 2nd link3rd
word_list = new ArrayList<>();
SQLiteDatabase sd = mydb.getWritableDatabase();
Cursor cursor = sd.query("stickerstable",null, null, null, null, null, null);
if (cursor.moveToFirst()){
do{
word_list.add(new data_items(
cursor.getInt(cursor.getColumnIndex(ID)),
cursor.getString(cursor.getColumnIndex(STICKER_AUTHOR)),
cursor.getString(cursor.getColumnIndex(STICKER_NAME)
)));
}while (cursor.moveToNext());
cursor.close();
sd.close();
}
You can use rawQuery instead of query
Cursor cursor = sd.rawQuery("SELECT YOUR_COLUMN FROM stickerstable",null);
replace YOUR_COLUMN with the name of column you want to retrieve you can even use your Constants like this
Cursor cursor = sd.rawQuery("SELECT " + ID + " FROM stickerstable",null);
or a better way to use String.format
Cursor cursor = sd.rawQuery(String.format("SELECT %s FROM stickerstable", ID),null);
UPDATE
you can use query and specify the column in the second argument
Cursor cursor = sd.query("stickerstable",new String[]{ID}, null, null, null, null, null);
when you pass null means get all columns
I am accessing Android MMS-SMS database:
Uri.parse("content://mms-sms/conversations?simple=true");
ContentResolver contentResolver = getContentResolver();
String[] projection = {"_id"};
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
//Is the id the same as the id in corresponding SMS or MMS table?
long id = cursor.getLong(cursor.getColumnIndex("_id"));
My question is simple, is the "_id" column holds the same id value of corresponding table (for SMS "content://sms"; for MMS "content://mms") ? If it is not, how can I get the id of the message in SMS or MMS table from the cursor I got?
What I want to do is I want to use this id to query the corresponding SMS or MMS table.
Do you want fetch mms-sms by "_id" column from "content://mms-sms/conversations?simple=true"
try
Cursor cursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations/"+id), new String[] {"_id","body","date","type","error_code","ct_t","_data","m_type"}, null, null, null);
"id" is value "_id" column.
I think you are doing that in wrong way. You should add your wanted values about sms to the projection like that;
String[] projection = {"Conversations._ID"};
then you can take sms id with that:
long id = cursor.getLong(0);
If you want to take another information from Sms, add your keys to projection like:
String[] projection = {"Conversations._ID", "Conversations.ADDRESS", "Conversations.BODY"};
then;
long id = cursor.getLong(0);
String address = cursor.getString(1); // tel number
String body = cursor.getString(2); // content of message
Edit: You should also check your uri and try that:
Cursor cursor = contentResolver.query(Conversations.CONTENT_URI, projection, null, null, null);
Good luck.
My intention is to display the contacts in sorting order using content resolver in android.
For that I'm writing:
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
It needs that the last parameter in query method should not be null for sorting the elements by Name. Which part of code I have to replace the null parameter to achieve sorting by name?
To sort result according to name use Phone.DISPLAY_NAME constant with ASC as last parameter to query method. do it as:
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",
new String[] { id },
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
It would be better to use SORT_KEY_PRIMARY or SORT_KEY_ALTERNATIVE on API level 11 and later.
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null,
ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
You can use Upper() to sort for both lower as well as upper case contact name.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
The ContentResolver.query() method takes many arguments but to sort the content provider records, you have to edit the last argument of this method.
It should be like this:
Cursor cursor=getContentProvider().query(.......,"DISPLAY_NAME ASC")
This will arrange the contacts in Ascending order of their name.
Note: This argument should be in a String datatype.
You can use the Desc string using these lines to sort out to see the recent contact
android.database.Cursor cursor = getContentResolver()
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null,
ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP +" DESC");
In sqLite,i have 3 values inserted in a table in my database ,from an activity.
I need to reteive those 3 values from the database,from another activity...The retreival of the first value from the table is possible..i cant retreive the remaining 2 values...How can the cursor object be defined to do the same???
myDB = MyService.this.openOrCreateDatabase("antitheft", MODE_PRIVATE, null);
Cursor cursor1 = myDB.query("SimSerial", null, null, null, null, null, null);
cursor1.moveToLast();
String ss1 = cursor1.getString(cursor1.getColumnIndex("simno"));
cursor2.moveToFirst();
String num1 = cursor2.getString(cursor2.getColumnIndex("secure"));
You can write the query for selecting all the records and then iterate through the records.
Cursor cursor1 = myDB.query("SimSerial", null, null, null, null, null, null);
cursor1.moveToFirst();
while(!cursor1.isAfterLast()){
cursor1.getString(....);
cursor1.moveToNext();
}
You need to iterate cursor. For that you can use following example.
Cursor cur = myDB.query("SimSerial", null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
String str = cur.getString(1);
cur.moveToNext();
}
cur.close();
I'm trying to query only the first data from the table.
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null",
null, null, null, null);
Could somebody tell me how to implement a query where only the first data is retrieved from a table?
Solution
I think I solved the answer:
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null",
null, null, null, null , "1");
I used limit 1 first, but the application crashed. Only if I pass the number as a String will it work. The query has 8 parameters while we're using the limit parameter.
limit 1
From Documentation:
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
so, put your last argument to be "limit 1".
Cursor img_cursor = db.query(
IMAGE_URL_TABLE_NAME,
new String[]{"small_img_url" , "large_img_url"},
null,
null, null, null, "limit 1");