My app queries a particular event in CalendarContract.Events using Events._ID. This worked well until attempting to run it on a 5.0 device, now I get an exception
01-12 17:28:50.525: E/Unknown Source(18499): android.database.sqlite.SQLiteException:
no such column: CalendarContract.Events._ID (code 1): ,
while compiling:
SELECT _id, account_type, title, organizer, description, eventLocation,
hasAlarm, calendar_id
FROM view_events
WHERE (lastSynced = 0 AND (CalendarContract.Events._ID=1))
Querying all columns in Events indeed does not return _ID. Any idea why this has been removed or if it's a bug? I can't seem to find away to uniquely identify events any more.
Here is my query:
String[] projection = new String[]{Events._ID, Events.ACCOUNT_TYPE, Events.TITLE,
Events.ORGANIZER, Events.DESCRIPTION, Events.EVENT_LOCATION, Events.HAS_ALARM,
Events.CALENDAR_ID};
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
"CalendarContract.Events._ID=" + eventId, null, null);
Thanks for any information!
The answer of #Andrew isn't right.
You are wrongly using the selectionClause and the selectionArgs parameters.
Here is what you are doing:
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
CalendarContract.Events._ID + "=" + eventId, null, null);
And here is what you should do:
Cursor cursor = context.getContentResolver().query(EVENTS_CONTENT_URI, projection,
CalendarContract.Events._ID + " = ?", new String[]{eventId}, null);
The eventId needs to be passed within the selectionArgs so that the contentResolver can build the query statement for you, chaining the selectionArgs and substituting them to the ? char in the selectionClause parameter.
The question is three years old, but I hope my answer can help someone else.
Related
If someone knows a better way to get a rowId from text in the row, please let me know.
I've been running around in circles with this and I know it's probably something simple, but I can't figure it out. Hoping someone can tell me what I'm doing wrong. I'm getting an error running this SQLite code:
String where = "SELECT rowid, * FROM masterRecord WHERE masNameCol=" + name;
Cursor c = db.query(true, masterName, ALL_KEYS_MASTER, where, null, null, null, null, null);
The error points to the second line.
"name" is a string variable (in this case it's "Mary"). The exact error I'm getting is:
SQLiteLog: (1) near "SELECT": syntax error in "SELECT DISTINCT _id, masNameCol, masTotalTimeCol FROM masterRecord WHERE SELECT rowid, * FROM masterRecord WHERE masNameCol=Mary"
I've tried every syntax change I could find and think of, and it never changes the error. I'm just trying to get the rowId of the row so I can change a value in another column.
Use rawQuery(), not query().
You are trying to specify the entire SQL statement, which is what rawQuery() is for. query() assembles the SQL statement from pieces, and your one piece (where) is not just the WHERE clause.
Use placeholders for queries:
where = "masNameCol = ?";
whereArgs = new String[] { name };
columns = new String[] { "rowId" , /* all other column names you are interested in */ };
Cursor c = db.query("mytable", columns, where, whereArgs, null, null, null);
I'm making an Android App with a sqlite db, which has a query method like this:
String[] projection = {COLUMN_NAME_ID};
String selection = COLUMN_NAME_TEAM + " = ?";
String[] selectionArgs = {teamId.toString()};
String sortOrder = COLUMN_NAME_NAME + " ASC";
Cursor c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
if (c.moveToFirst()){
//...
}
as suggested here. The point is to implement this query:
SELECT PLAYER_ID FROM PLAYERS WHERE TEAM_ID = ? ORDER BY NAME ASC;
According to the SQLiteDatabase.query documentation, this is a valid call to query(), and so far I've been doing it successfully with all my other queries in this App.
For some reason, in this query (and only in this) the App freezes in c.moveToFirst().
Some answers to similar questions suggest that it might be a performance issue. I don't think this is the case, as my table has only 6 rows and the query is quite simple.
Any ideas?
Thanks
The application has a SearchView which fetches suggestions from a specific database table. Everything worked without any errors until Android 5.0 appeared.
As of then, when the SQLiteQueryBuilder queries the database to fill the Cursor object, the return is empty cursor. Not NULL, but empty.
On other platforms, I can output the Cursor's content via DatabaseUtils.dumpCursorToString(cursorObject), but on Android 5.0+ the method reports output on null objects
Dumping cursor null
<<<<<
Even more: when I extract database file from 5.0+ devices and run the local SQL query, I can fetch all data. So the database is valid indeed. And the query is the simplest one
SELECT rowid AS _id, suggest_text_1, suggest_text_2, rowid AS suggest_intent_data_id
FROM fts
WHERE (fts MATCH '*e*') //<-- I pressed "e" on the keyboard
The logic for selecting data from the database and filling the Cursor object is really simple
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(FTS3_TABLE);
builder.setProjectionMap(mColumnMap);
Cursor cursor = builder.query(db,columns, selection, selectionArgs,
null, null, null);
I tried looking for some deprecated methods, but was without any luck.
I have already spent 3 days debugging each step in the process and I am out of any ideas what could be causing such behaviour.
Anyone has any ideas?
EDIT
The output of the method buildQuery()
String query = builder.buildQuery(columns, selection, null, null, null, null);
RESULT:
SELECT rowid AS _id, suggest_text_1, suggest_text_2, rowid AS suggest_intent_data_id
FROM fts
WHERE (fts MATCH ?)
selection and selectionArgs parameters are created like this
String selection = FTS3_TABLE + " MATCH ?";
String[] selectionArgs = new String[]{"*" + query + "*"};
The RAW query resulted the same thing as query via builder.query()
String query = builder.buildQuery(columns, selection, null, null, null, null);
Cursor temp = db.rawQuery(query, selectionArgs);
String output = DatabaseUtils.dumpCursorToString(temp);
Output: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor#295023a7
<<<<<
Answered https://stackoverflow.com/a/30710226/437039 by laalto
MATCH '*foo*' queries never worked correctly in any version of sqlite. The fact that you got some results earlier was just a coincidence. Just the prefix form MATCH 'foo*' (and MATCH 'foo') are supported.
Lollipop ships with a newer version of sqlite. For detailed list of changes between sqlite versions, see the changelog.
Have you tried to query using the SQLiteDatabase query method like the following?
String selection = "fts" + " =? AND " +
String[] selectionArgs = new String [] {
"*e*"
};
String[] projection = new String [] { "rowId", "suggested_text"};
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor cursor = db.query("tableName",
projection,selection, selectionArgs, null, null, null);
I'm having trouble retrieving songs from playlists in Android. I am currently attempting to achieve this using the following code (based on an answer to this question given here):
String where = MediaStore.Audio.Playlists._ID
+ "=?";
String whereVal[] = {id};
String[] proj =
{
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members._ID
};
Cursor mCursor = getActivity().getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Long.parseLong(id)),
proj, where, whereVal, null);
where id is the playlist ID that I retrieve using cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID)); (and I know that this is correct)
However this is causing the following error:
07-06 19:35:02.496: E/AndroidRuntime(8818): android.database.sqlite.SQLiteException:
ambiguous column name: _id (code 1): , while compiling: SELECT audio_id, artist, title,
audio_playlists_map._id AS _id FROM audio_playlists_map, audio WHERE (audio._id =
audio_id AND playlist_id=?) AND (_id=?)
I have a funny feeling that the problem lies in the following:
String where = MediaStore.Audio.Playlists._ID + "=?"; but I have no clue how to rectify it.
You don't need the where clause since you already use the content URI of the playlist. I would try this:
Cursor mCursor = getActivity().getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Long.parseLong(id)),
proj, null, null, null);
I have variable:
String owner="Mike";
String[] columns ={"quantity", "price","owner"}
My cursor is trying to get
Cursor findEntry = db.query("sku_table", columns, "owner="+owner, null, null, null, null);
I got an error no such column error
android.database.sqlite.SQLiteException: no such column: owner: , while compiling: SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike
But if I take this query:
SELECT quantity, price, owner, FROM sku_table WHERE owner=Mike
and add "" to Mike, and tested in sqlite browsers to execute the query, I do get back the row.
The working query looks like this:
SELECT quantity, price, owner, FROM sku_table WHERE owner="Mike"
Can somebody drop some insights about how do I incorporate double quotes? Other than use \"
Thanks!
Sorry, but that is exactly the reason why you should work with what the method offers! #Leandros and #Jake are helping in the totally wrong direction! Sorry to say that...
The only solution you should use is this:
Cursor findEntry = db.query("sku_table", columns, "owner=?", new String[] { owner }, null, null, null);
ps: Yes I down voted both answers as they may work but providing a solution that shouldn't be used.
Update:
If you need more than one where condition, just add it like you would do in a normal query
Cursor findEntry = db.query("sku_table", columns, "owner=? and price=?", new String[] { owner, price }, null, null, null);
The order of the ? and the new String[] {...} elements must be the same!
Cursor findEntry = db.query("sku_table", columns, "owner='"+owner+"'", null, null, null, null);
SELECT quantity, price, owner, FROM sku_table WHERE owner='Mike' this is the correct SELECT. You forget the ' ' (single quotes)
public Cursor show_vol(String vol,String bk,String hadnu)
{
SQLiteDatabase db = this.getReadableDatabase();
String[] columns ={"hadith"};//colums name that you select
Cursor res = db.query("volume2", columns, "hadithno=?", new String[] { hadnu }, null, null, null);
//volume2 is table name and hadithno is colume name l
//select hadith from volume2 where hadithno=hadnu //working like s
1. List item
return res;
}
I know this is an old question, but you can also do it like this:
Cursor findEntry = db.query("sku_table", columns, "owner=\'"+owner+"\'", null, null, null, null);
I just did it in my app and it worked as expected.
Jake's answer was similar, but probably wouldn't work without the \ before the '
the simplest way is to use SELECT col1,col2 FROM table_name WHERE col =' something' ; just like Leandros said , my problem was the single quotes , thnx