I am developing an application when i retrieve a data from an sqlite database using rawQuery method because the query is a UNION query and the data is displayed on a listview . I would like to use a query method because i would also like to implement a content provider to handle the data retrieved from the database.
Is there any way someone can implement a union query using the query method.
I would only like to know of the structure since the query is already working using the rawQuery method how do i structure it using a query method
You can use following methods:
public String buildUnionQuery (String[] subQueries, String sortOrder, String limit)
public String buildQuery (String[] projectionIn, String selection, String groupBy, String having, String sortOrder, String limit)
For SQL queries for Union.
check the below links
http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
How to use rawQuery() in android
I've answered a similar question, take a look at my complete answer.
TL;DR: I think it doesn't currently exist any way to use a query instead of rawquery when you want to do a UNION query.
Related
How can I translate the following Orderby sql statement to
ORDER BY Country ASC, CustomerID DESC;
I have manage to write Ordering.property("Country").ascending();
but not sure how to add the next condition.
Thanks
If you look at the orderBy method, you will see that it has the following signature
public OrderBy orderBy(Ordering... orderings)
Note that you can put multiple Ordering objects inside.
This question already has answers here:
Android SQLite: Which query ("query" or "rawQuery") is faster?
(2 answers)
Closed 8 years ago.
I am learning to implement databases in android , i came across this query() and rawQuery() , i want to know the difference between them and which one is efficient in android
From API Doc:
public void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
public Cursor rawQuery (String sql, String[] selectionArgs)
Runs the provided SQL and returns a Cursor over the result set.
If you want to e.g. CREATE TABLE that does not return values you can use execSQL(), if you want a Cursor as result use rawQuery() (=SELECT statements).
Looking at SQLiteDatabase.java in the android source shows that the query(..) ends up calling the QueryBuilder to build the query as a single string and then it essentially calls rawQuery(). They should be roughly equivalent, assuming that you also did the same work to build your own statement.
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.
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.