I'm working on a SMS application and I faced some problems retrieving the data from the ContentProvider using the GROUP BY clause.
I have read a lot of threads of similar questions, but they still do not provide a working solution for me.
What I want, is to get the most recent SMS from every thread. To achive this I have to select the maximum value of DATE column, and group it by THREAD_ID, I believe.
Suppose the SMS table looks like this:
============================
THREAD_ID | MESSAGE | DATE |
============================
1 | Hello | 555
1 | Hi | 666
2 | Test | 333
2 | Test 2 | 999
And the result should return the:
1 - Hi - 666
2 - Test 2 999
This is what I have so far, but the below code retrieves the most recent SMS from the whole table, not from specific category. It look like the GROUP BY clause is neglected.
String CONTENT_URI = "content://sms/";
Uri uri = Uri.parse(CONTENT_URI);
String selection = "date=(SELECT max(date) FROM sms )";
selection+=") GROUP BY (thread_id";
Cursor c = managedQuery(uri, null, selection, null, null);
if (c != null) {
while (c.moveToNext()) {
textView.append(c.getString(c.getColumnIndex(Column.THREAD_ID))
+ " - " + c.getString(c.getColumnIndex(Column.ADDRESS))
+ " - " + c.getString(c.getColumnIndex(Column.READ))
+ " - " + c.getString(c.getColumnIndex(Column.BODY))
+ " - " + c.getString(c.getColumnIndex(Column.DATE))
+ "\n");
}
c.close();
} else {
textView.setText("Cursor is null");
}
Thank you in advance, I would appreciate very much if you could help me find a solution for this case.
The GROUP BY clause is neglected because SELECT give only 1 value (in your code).
I don't know very well SQL but I think this should work (sry, I don't know exact syntax)
SELECT thread, message, MAX(date)
FROM sms
GROUP BY thread
use a library like CPH here to do all the boilerplate stuff for you. The example code shows all the query stuff that you need.
Implement as following
public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
/* a String is a Object, so it can be null!*/
String groupBy = null;
String having = null;
switch (sUriMatcher.match(uri)) {
...
...
...
case EPISODES_NEXT:
groupBy = "ShowID";
queryBuilder.setTables(EpisodenTable.TableName);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Cursor c = queryBuilder.query(db, projection, selection, selectionArgs,
groupBy, having, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
Related
I have code that fetches contact information from Android using the contact name as the selection. However, the datasource (which I guess would be ContactsContract.Contacts) does not have everything I need about the contact. I see that contact information is divided between several tables (ContactsContract.CommonDataKinds.StructuredName, ContactsContract.CommonDataKinds.Phone, etc). I have also notices that all of these sources have access to a LOOKUP_KEY field. It uniquely identifies a contact. I want to know if there is a way to perform a join on these datasources using the LOOKUP_KEY.
Below, I currently setup the projection and selection for a single datasource and query it asynchronously using a cursor loader. Is there a way to search multiple datasources at once using the LOOKUP_KEY?
private static final String[] CONTACT_PROJECTION = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
private static final String CONTACT_SELECTION = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
+ " LIKE ?";
public static Loader<Cursor> getContactLoader(Context applicationContext, String searchString) {
String[] selectionArgs = new String[1];
selectionArgs[0] = "%" + searchString + "%";
return new CursorLoader(
applicationContext,
ContactsContract.Contacts.CONTENT_URI,
CONTACT_PROJECTION,
CONTACT_SELECTION,
selectionArgs,
null
);
}
All info on all contacts is grouped in alias tables, which are actually stored in a single big table called Data, so you can simply query that table for all info related to a specific contact (you can use CONTACT_ID, no need to mess with the more problematic LOOKUP_URI), like so:
public void getContactInfo(long contactId) {
Uri uri = Data.CONTENT_URI;
String[] projection = new String[]{
Data.CONTACT_ID,
Data.DISPLAY_NAME,
Data.MIMETYPE,
Data.DATA1}; // you can get additional info if needed in Data.DATA2, DATA3, etc.
// get all relevant info about the contact
String selection = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + " IN (?,?,?,?)"; // you can add more question marks if you need more MIMETYPES
String[] selectionArgs = new String[]{String.valueOf(contactId), Phone.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE, Event.CONTENT_ITEM_TYPE, StructuredPostal.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
while (cursor.moveToNext()) {
String name = cursor.getString(1);
String mimetype = cursor.getString(2);
String data = cursor.getString(3);
// use the mimetype to figure out what kind of info is in this row
Log.i("Contact Info", contactId + "=> " + name + " - " + mimetype + ": " + data);
}
cursor.close();
}
so i recently learn to write a code in android using sqlite and i try to select data from sqlite but this error occur
ive tried some suggestion from the internet and read my book but i didnt solve my problem
public Penyakit getPenyakit1(String namaGejal){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT idPen FROM " + TABLE_CONTACTS + " WHERE " +
namapen + " =\"" + namaGejal + "\"";
Cursor cursor = db.rawQuery(query,null);
Penyakit penyakit = new Penyakit();
if(cursor.moveToFirst()){
cursor.moveToFirst();
penyakit.set_nomber(Integer.parseInt(cursor.getColumnName(0)));
penyakit.set_namaPen(cursor.getColumnName(1));
penyakit.set_idPenyakit(Integer.parseInt(cursor.getColumnName(2)));
penyakit.set_namGej(cursor.getColumnName(3));
penyakit.set_idGejala(Integer.parseInt(cursor.getColumnName(4)));
cursor.close();
} else {
penyakit=null;
}
return penyakit;
}
this is logcat
Process: com.example.lordbramasta.pakar, PID: 18914
java.lang.NumberFormatException: For input string: "idPen"
at java.lang.Integer.parseInt(Integer.java:615)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.lordbramasta.pakar.DBAdapter.getPenyakit1(DBAdapter.java:79)
i expected the value of idPen get selected , thank you
Your problem is this line:
penyakit.set_nomber(Integer.parseInt(cursor.getColumnName(0)));
cursor.getColumnName(0) returns idPen as this is the name of the only column returned by your query:
SELECT idPen FROM ....
and your code is trying to cast the string "idPen" to an integer.
So getColumnName() returns the name of the column at a specified index and not the value of the column.
You should do
penyakit.set_nomber(Integer.parseInt(cursor.getString(0)));
or if the data type of the column idPen is INTEGER then:
penyakit.set_nomber(cursor.getInt(0));
Also don't try to get any other columns because your query returns only 1.
Note: remove that cursor.moveToFirst(); inside the if block because it is already executed.
Probably you need to use a ' instead of ". So, change the query to the following:
String query = "SELECT idPen FROM " + TABLE_CONTACTS + " WHERE " +
namapen + " =\'" + namaGejal + "\'";
I'm suggesting you to use SQLiteDatabase.query() instead rawQuery like this:
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
"idPen"
};
// Filter results WHERE "namapen" = 'namaGejal'
String selection = "namapen" + " = ?";
String[] selectionArgs = { namaGejal };
// How you want the results sorted in the resulting Cursor
String sortOrder = null; // null for default order
Cursor cursor = db.query(
TABLE_CONTACTS, // The table to query
projection, // The array of columns to return (pass null to get all)
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
// do something with the cursor
Please take a look Read information from a database
If you want to get all columns data from your TABLE_CONTACTS use SELECT * FROM
I've got very strange problem.
My snippet is looking like that
public Wynik getData(int pomiar, int godzina) {
Wynik wynik = null;
String query = "SELECT * FROM " + "insulina" + " WHERE godzina = " + godzina +
" AND cukier = " + pomiar;
SQLiteDatabase db = this.getReadableDatabase();
try (Cursor cursor = db.rawQuery(query, null)) {
Log.i("tag", "cursor length:" + cursor.getCount());
if (cursor.getCount() > 0) {
String s = DatabaseUtils.dumpCursorToString(cursor);
Log.d("s", s);
cursor.moveToFirst();
wynik = new Wynik
(cursor.getInt(cursor.getColumnIndexOrThrow("godzina")),
cursor.getString(cursor.getColumnIndexOrThrow("insulina")),
cursor.getInt(cursor.getColumnIndexOrThrow("dawka")),
cursor.getString(cursor.getColumnIndexOrThrow("cialo")));
}
cursor.close();
return wynik;
(sorry for not English convention, but it was meant to be quick, routinely project)
Whole app idea is to help my friend with his grandmother's diabetes. He would insert an result of blood sugar level test (cukier) and time of test is checked automatically (godzina).
Based on those, app should return a whole result (wynik), with required dose of insulin (dawka), type of it (insulina) and part of body from which the blood should be taken (cialo).
Database looks like this:
So for example. The blood result is 10 (it's only thing which user is inserting) and it's 7 P.M (19, by 24-hours convention - the program is giving the closest hour of test, so it's 18). So, the query looks like
SELECT * FROM insulina WHERE godzina = 18 AND cukier = 10
And here's my problem. The app is recognizing cursor length as 0 (it should be 1) even if my other app related to database creation (DB Browser for SQLite) is giving out my result properly with the same query
Also Android Studio says that database is sucessfully opened.
I cannot find a fault here. And I'm really confused with this problem.
I think the query should be :
String query = "SELECT * FROM insulina WHERE godzina = ? AND cukier = ?";
SQLiteDatabase db = this.getReadableDatabase();
try (Cursor cursor = db.rawQuery(query, new String[]{String.valueOf(godzina), String.valueOf(pomiar)})) {
//... rest of code
Also looks like you're using "try with resources" so the close isn't explicitly necessary.
Instead of using direct query to db im using contentResolver but it returns null. And i dont know do i need to add some code or there is a mistake in mine.
private void displayDatabaseInfo() {
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
PetEntry._ID,
PetEntry.COLUMN_PET_NAME,
PetEntry.COLUMN_PET_BREED,
PetEntry.COLUMN_PET_GENDER,
PetEntry.COLUMN_PET_WEIGHT };
// Perform a query on the provider using the ContentResolver.
// Use the {#link PetEntry#CONTENT_URI} to access the pet data.
Cursor cursor = getContentResolver().query(
PetEntry.CONTENT_URI, // The content URI of the words table
projection, // The columns to return for each row
null, // Selection criteria
null, // Selection criteria
null); // The sort order for the returned rows
Log.v(LOG_TAG, "Проверка курсора " + cursor);
TextView displayView = (TextView) findViewById(R.id.text_view_pet);
if (cursor != null){
try {
// Create a header in the Text View that looks like this:
//
// The pets table contains <number of rows in Cursor> pets.
// _id - name - breed - gender - weight
//
// In the while loop below, iterate through the rows of the cursor and display
// the information from each column in this order.
displayView.setText("The pets table contains " + cursor.getCount() + " pets.\n\n");
displayView.append(PetEntry._ID + " - " +
PetEntry.COLUMN_PET_NAME + " - " +
PetEntry.COLUMN_PET_BREED + " - " +
PetEntry.COLUMN_PET_GENDER + " - " +
PetEntry.COLUMN_PET_WEIGHT + "\n");
// Figure out the index of each column
int idColumnIndex = cursor.getColumnIndex(PetEntry._ID);
int nameColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);
int breedColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
int genderColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER);
int weightColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);
// Iterate through all the returned rows in the cursor
while (cursor.moveToNext()) {
// Use that index to extract the String or Int value of the word
// at the current row the cursor is on.
int currentID = cursor.getInt(idColumnIndex);
String currentName = cursor.getString(nameColumnIndex);
String currentBreed = cursor.getString(breedColumnIndex);
int currentGender = cursor.getInt(genderColumnIndex);
int currentWeight = cursor.getInt(weightColumnIndex);
// Display the values from each column of the current row in the cursor in the TextView
displayView.append(("\n" + currentID + " - " +
currentName + " - " +
currentBreed + " - " +
currentGender + " - " +
currentWeight));
}
} finally {
// Always close the cursor when you're done reading from it. This releases all its
// resources and makes it invalid.
cursor.close();
}
}
}
Before that i got an error null exception. After added if/else statement exceptions dissapeared but anyway it doesnt work and returns null
Problem was in ContentProvider. I have changed code and now it works.
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
// Get readable database
SQLiteDatabase database = mDbHelper.getReadableDatabase();
// This cursor will hold the result of the query
Cursor cursor;
// Figure out if the URI matcher can match the URI to a specific code
int match = sUriMatcher.match(uri);
switch (match) {
case PETS:
// For the PETS code, query the pets table directly with the given
// projection, selection, selection arguments, and sort order. The cursor
// could contain multiple rows of the pets table.
cursor = database.query(PetEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
case PET_ID:
// For the PET_ID code, extract out the ID from the URI.
// For an example URI such as "content://com.example.android.pets/pets/3",
// the selection will be "_id=?" and the selection argument will be a
// String array containing the actual ID of 3 in this case.
//
// For every "?" in the selection, we need to have an element in the selection
// arguments that will fill in the "?". Since we have 1 question mark in the
// selection, we have 1 String in the selection arguments' String array.
selection = PetEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
// This will perform a query on the pets table where the _id equals 3 to return a
// Cursor containing that row of the table.
cursor = database.query(PetEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Cannot query unknown URI " + uri);
}
return cursor;
}
I have the below sqlite query running, I know it many not be the best way to do it, but I am just trying to get this to work at the moment.
If I run these two queries I get results.
Cursor value = checkDB.rawQuery("SELECT * FROM table WHERE open ='1'", null);
Cursor value = checkDB.rawQuery("SELECT * FROM table WHERE country='"+ countryID +"'", null);
But if I combine the two where clauses I get no results. Have I over looked something simple?
Cursor value = checkDB.rawQuery("SELECT * FROM table WHERE open ='1' AND country='"+ countryID +"'", null);
It turns out that I should have considered it was something to do with being in an expandable listview. Below is the full code where the query is nested. The odd thing is the countryID always outputs 1, but when you click on a different group the correct coutries are listed.
protected Cursor getChildrenCursor(Cursor groupCursor) {
String countryID = Integer.toString(groupCursor.getInt(0));
SQLiteDatabase checkDB = null;
checkDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
Cursor value = checkDB.rawQuery("SELECT * FROM table WHERE open ='1' AND countries='"+ countryID +"'", null);
String test = "";
if(value.moveToFirst())
test = value.getInt(0) + ": " + value.getString(1);
while(value.moveToNext()){
test += ";" + value.getInt(0) + ": " + value.getString(1);
}
checkDB.close();
return value;
}
Your queries are fine, and the results you're getting are perfectly valid. Consider the following situation:
table:
| open | country |
_____________________________
| 0 | <your country id> |
| 1 | <another id> |
Then your two first queries would indeed return some result, but your third would not.
Check your data, and make sure you actually have rows with open == 1 and the correct country id at the same time.
If you wish to return all the rows from either one of your first two queries, then you should turn your AND into an OR. And you will get the combined results of your two first queries (in my example table above, the two rows)
i.e.
Cursor value = checkDB.rawQuery("SELECT * FROM table WHERE open ='1' OR country='"+ countryID +"'", null);
There is nothing wrong with the query itself, but are you sure there are rows that has columns with open=1 AND country=whateverthevalue, at the same time? If not, you will of course not get any result.
Run "adb shell", open your databasefile with sqlite3 and run the query there to check.