I want to do a rawquery and sort the result according to some alphanumeric value. Like I have column ItemId which contains values like A0001,A0002,A0036,B0085 etc.
String query="select S_PriceP,ItemId,ItemName from PDAProduct where ItemId<=A0001 and ItemId>=A0099 order by ItemId";
Is there any way I can achieve this?
You can use rawQuery.
String query="select S_PriceP,ItemId,ItemName from PDAProduct where ItemId<=A0001 and
ItemId>=A0099 order by ItemId";
Cursor objCursor = objSQLiteDatabase.rawQuery(query, null);
With the little info you have given us, my best answer would be to look at SQLiteDatabase docs... in particular, you want one of the query methods. If you were to post some of the code you have tried we might be able to find what you're missing.
Related
I want to make SELECT * FROM my table and i don't want to sync colnames with colindex.
Is there a built-in way to fetch column names onto Cursor with columnNames instead of columnIndexes?
So i can use cursor.getStringForColumnName("name"); instead of knowing that 4th column is "name" column and using cursor.getString(4);
What I understood is that you want to use coulmn name in Cursor. You can actually use getColumnIndex(String columnName) to get the index automatically using column name. Read more here. Then you can continue with the operations. This way you don't need to remember the indexes. hope this helps else please comment.
well, YES you can use PRAGMA query for this purpose
Here, is an example
Cursor ti = db.rawQuery("PRAGMA table_info(mytable)", null);
if ( ti.moveToFirst() ) {
do {
System.out.println("col: " + ti.getString(1));
} while (ti.moveToNext());
}
cursor.getString(getColumnIndex("name"));
I like execSQL because i can do this:
db.execSQL("INSERT INTO Usuarios (codigo, nombre) VALUES (1, 'pedro')");
Does there exist something similar (which only needs a string as parameter) that can process SELECT statements and return a Cursor object with the results?
This is not exactly what you want, but is very similar:
public Cursor rawQuery (String sql, String[] selectionArgs);
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery%28java.lang.String,%20java.lang.String[]%29
Just use '?' during the query for the values you want later to substitute with the String array in the correct order.
You should learn to use the query method, there is a reason why they added it to the frameworks. Also look up SQLiteQueryBuilder.
You can use Raw Query. In Raw query you can put string of SQL query.
I am looking to perform the following query (in pseudo-code) on Android:
SELECT C.ID, C.NAME, CASE ISNULL(G.GROUPID,0) = 0 THEN 0 ELSE 1 END INGROUP
FROM CONTACTS C
LEFT JOIN GROUPMEMBERSHIP G ON G.CONTACTID = C.ID AND G.GROUPID = ?
I am looking to select the ID and Name of ALL contacts in the system address book, via the default Contacts ContentProvider, along with a
0/1 field indicating whether the contact is a member of group ? .
I could of course get all contacts easily enough, then loop through and query the membership separately easy enough in my Adapter class, but I'd imagine performing the two queries as one outer joined query would yield much better performance.
Can I do this with the standard high-level string-projection and ContentResolver.query() method? Or would this kind of query require digging into more direct SQL execution?
Edit: Okay, so this doesn't actually solve the question asked, because eidylon is tied to an existing ContentProvider as mentioned in their question. However, this does cover how you do a JOIN if you own the ContentProvider source and API. So I'll leave it for those who want to know how to handle that case.
This is easy! But unintuitive... :)
query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
Okay, so what is URI? Typically, you have one URI per table.
content://com.example.coolapp.contacts serves data out of your CONTACTS table.
content://com.example.coolapp.groupmembers serves data out of your GROUPMEMBERSHIP table.
But URI is really just a string. Use it however you like. Make a block of code in your ContentProvider that responds to content://com.example.coolapp.contacts_in_group. Within that block of code in the ContentProvider, you can get raw access to your SQLite DB, unfettered by the limited query() data model. Feel free to use it!
Define your selection fields however you like. They don't have to map to table column names -- map them how you need to, in order to get your parameters in.
Define your projection how you need -- It may contain columns from both tables after the join.
Bing, you're done. Google does this same model internally in their own code -- Go look at the Contacts provider API -- you see "bla.RawContact" and "bla.Contact" and etc as content URIs. Each serves data out of the same table in the DB -- the different URIs just provide different views of that same table!
Nope, you can't do that kind of queries with the ContentResolver.query() method.
You will need to write something like this:
SQLiteDatabase db = YourActivity.getDbHelper().getReadableDatabase();
String query = yourLongQuery;
Cursor c = db.rawQuery(query, null);
YourActivity.startManagingCursor(c);
c.setNotificationUri(YourActivity.getContentResolver(), YourContentProvider.CONTENT_URI);
You can't do that because ContentResolver has only one query method:
query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
there's no parameter for tables or FROM clauses.
i am sorry to ask again since i asked something similar to this earlier, but am in serious need of help here. situation is this, i have an Arraylist with values of strings, which i have converted to an array i want to query a "NAME" column in my sqlite database and return rows that have the string values in the name column.
e.g the `String [] a = {"do this", "do that", "help here"}; but it grows dynamically and can have a lenght with respect to the database column.(i mean it can't grow past the size of the database table.)
how can i construct an sqlite query to return rows that have this string in their array? Am having sqlite error of "incorrect syntax" or "bind or column index out of bounds" when using the suggestions from this post.
[http://stackoverflow.com/questions/3985263/using-an-array-to-query-an-sqlite-database-android][1]
i have no idea how to query the table anymore. please help, any help will be greatly appreciated. Thank you
In a nutshell: The function you want is ContentResolver.query(). You get the resolver via context.getContentResolver().
You pass in:
Uri - the URI of your content provider.
Projection - the columns you want.
selection - okay, that's where it gets interesting. Create a string here that looks like this: NAME=? OR NAME=? OR NAME=?.
selectionArgs - this is essentially the string array a in your question.
sortOrder - whatever you want.
If you're not using a content provider, you can use SQLiteDatabase's query function which works in an almost identical fashion.
Android's API provides a clean mechanism via SQLite to make queries into the contact list. However, I am not sure how to limit the results:
Cursor cur = ((Activity)mCtx).managedQuery(
People.CONTENT_URI,
columns,
"LIMIT ? OFFSET ?",
new String[] { Integer.toString(limit), Integer.toString(offset) },
null
);
Doesn't work.
Actually, depending on the provider you can append a limit to the URI as follows:
uri.buildUpon().appendQueryParameter("limit", "40").build()
I know the MediaProvider handles this and from looking at recent code it seems you can do it with contacts too.
You are accessing a ContentProvider, not SQLite, when you query the Contacts ContentProvider. The ContentProvider interface does not support a LIMIT clause directly.
If you are directly accessing a SQLite database of your own, use the rawQuery() method on SQLiteDatabase and add a LIMIT clause.
I found out from this bug that Android uses the following regex to parse the LIMIT clause of a query:
From <framework/base/core/java/android/database/sqlite/SQLiteQueryBuilder.java>
LIMIT clause is checked with following sLimitPattern.
private static final Pattern sLimitPattern = Pattern.compile("\\s*\\d+\\s*(,\\s*\\d+\\s*)?");
Note that the regex does accept the format offsetNumber,limitNumber even though it doesn't accept the OFFSET statement directly.
I think you have to do this sort of manually. The Cursor object that is returned from the managedQuery call doesn't execute a full query right off. You have to use the Cursor.move*() methods to jump around the result set.
If you want to limit it, then create your own limit while looping through the results. If you need paging, then you can use the Cursor.moveToPosition(startIndex) and start reading from there.
You can specify the "limit" parameter in the "order" parameter, maybe even inside other parameters if you don't want to sort, because you'll have to specify a column to sort by then:
mContentResolver.query(uri, columnNames, null, null, "id LIMIT 1");