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.
Related
What is the exact difference between using rawquery and execSQL ??
While writing a query in android activity, when to use rawquery and when to use execSQL ?
From API documentation:
void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
The documentation is inconsistent but they behave both the same. Documentation of the latter is more in depth.
Cursor rawQuery (String sql, String[] selectionArgs)
Runs the provided SQL and returns a Cursor over the result set.
Uses for rawQuery are:
You want to query the database with a SELECT statement.
=> rawQuery("SELECT ... returns a set of rows and columns in a Cursor.
It's more efficient to use DatabaseUtils.longForQuery(SQLiteDatabase, String, String[]) or DatabaseUtils.stringForQuery(...) in cases there is only a 1x1 query result, like from SELECT count(*) FROM table (which also has it's own dedicated method: DatabaseUtils.queryNumEntries(...)) - this skips creation of a Cursor object & simplifies code since there is also nothing to close, moveToNext, etc.
Special cases like PRAGMA table_info that returns data in rows (see this question)
Note: Do not use rawQuery for INSERT, UPDATE or DELETE or anything else that modifies the database. You'll run into "Why does a delete rawQuery need a moveToFirst in order to actually delete the rows?". Reason being that queries can defer reading the result until needed (= access to the cursor) which means for SQLite delaying execution of the statement.
Uses for execSQL are:
You have "instructions" for the database. Like CREATE TABLE (or any other CREATE statement, e.g. CREATE INDEX), DROP, PRAGMAs that set properties rather than returning them, ...
INSERT, UPDATE or DELETE when you're not interested in the amount of rows modified or the row id of the last insert.
When you need those, either use the update(), insert(), delete() methods or use a second statement to read those: DatabaseUtils.longForQuery with either SELECT last_insert_rowid() or SELECT changes(). Both return only 1 integer value. (see "Get updated rows count from SQLite in Android using a raw query?" and “SELECT last_insert_rowid()” returns always “0”)
Anything else that relies on executing a statement.
if you want to execute something in database without concerning its output (e.g create/alter tables), then use execSQL, but if you are expecting some results in return against your query (e.g. select records) then use rawQuery
I'm curious about the design of the Android SQLite API. For example, we have
public void execSQL(String sql, Object[] bindArgs)
for SQLs that don't return data, and we also have
public Cursor rawQuery(String sql, String[] selectionArgs)
for SQLs that do return data.
Why are the arguments expected as an Object array in the former and as a String array in the latter case? I understand that accepting only Strings might make sense due to SQLite's type affinity. However, if that is the case, why not consistently use String arrays for arguments? Is there some non-String argument that makes sense for execSQL but not for rawQuery?
Actually, accepting only strings does not make sense because strings never compare equal with numbers, i.e., queries like
c.rawQuery("SELECT * FROM mytable WHERE some_number_column = ?", args);
will never return any records with numbers. (You would have to declare all columns as TEXT, and never use expressions or convert them with something like CAST(... AS TEXT).)
Jens mentioned a possible explanation, but that is no excuse for inconsistent and plain bad design.
There is one. For example if you want to compare BLOB values. Have a look at this example.
UUID stored as BLOB
db.execSQL("DELETE FROM "+USER_TABLE+" WHERE "+USER_UUID+"=?",
new Object [] { uuid.toByteArray }
According to documentation, byte[], String, Long and Double are supported in bindArgs. However, it is not recommended to use this method for executing SELECT/INSERT/UPDATE/DELETE statements.
I just don't know why.
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.
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.
What is the exact difference between using rawquery and execSQL ??
While writing a query in android activity, when to use rawquery and when to use execSQL ?
From API documentation:
void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
The documentation is inconsistent but they behave both the same. Documentation of the latter is more in depth.
Cursor rawQuery (String sql, String[] selectionArgs)
Runs the provided SQL and returns a Cursor over the result set.
Uses for rawQuery are:
You want to query the database with a SELECT statement.
=> rawQuery("SELECT ... returns a set of rows and columns in a Cursor.
It's more efficient to use DatabaseUtils.longForQuery(SQLiteDatabase, String, String[]) or DatabaseUtils.stringForQuery(...) in cases there is only a 1x1 query result, like from SELECT count(*) FROM table (which also has it's own dedicated method: DatabaseUtils.queryNumEntries(...)) - this skips creation of a Cursor object & simplifies code since there is also nothing to close, moveToNext, etc.
Special cases like PRAGMA table_info that returns data in rows (see this question)
Note: Do not use rawQuery for INSERT, UPDATE or DELETE or anything else that modifies the database. You'll run into "Why does a delete rawQuery need a moveToFirst in order to actually delete the rows?". Reason being that queries can defer reading the result until needed (= access to the cursor) which means for SQLite delaying execution of the statement.
Uses for execSQL are:
You have "instructions" for the database. Like CREATE TABLE (or any other CREATE statement, e.g. CREATE INDEX), DROP, PRAGMAs that set properties rather than returning them, ...
INSERT, UPDATE or DELETE when you're not interested in the amount of rows modified or the row id of the last insert.
When you need those, either use the update(), insert(), delete() methods or use a second statement to read those: DatabaseUtils.longForQuery with either SELECT last_insert_rowid() or SELECT changes(). Both return only 1 integer value. (see "Get updated rows count from SQLite in Android using a raw query?" and “SELECT last_insert_rowid()” returns always “0”)
Anything else that relies on executing a statement.
if you want to execute something in database without concerning its output (e.g create/alter tables), then use execSQL, but if you are expecting some results in return against your query (e.g. select records) then use rawQuery