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 ?
RawQuery is for people who understand SQL and aren't afraid of it, query is for people who don't.
On the other hand, RawQuery serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into objects. RawQuery methods must return a non-void type. If you want to execute a raw query that does not return any value, use RoomDatabase#query methods.
ExecSQL(String) Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data. ExecSQL(String, Object[]) Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
there is no difference between SQLiteOpenHeloper::close() and SQLiteDatabase::close() . SQLiteOpenHeloper::close() is just a wrapper around SQLiteDatabase::close() . but as a rule of thumb, either let SQLiteOpenHelper manages your connections, or don't use it and manage it yourself. see this blog post.
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:
SELECT
statement.rawQuery("SELECT ...
returns a set of rows and columns in a Cursor
. 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.PRAGMA table_info
that returns data in rows (see this question) 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:
CREATE TABLE
(or any other CREATE
statement, e.g. CREATE INDEX
), DROP
, PRAGMA
s 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. 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”)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With