Android Delete Query - android

I'm attempting to delete all rows in one table that do not have a corresponding ID in another table. Since apparently SQLite does not support joins in deletes I am trying to do something along these lines:
DELETE FROM my_table WHERE my_id NOT IN (SELECT _id FROM my_table2);
However, I apparently can not use rawQuery since it returns a cursor so I have to use the delete function. I'm having some trouble getting this working. Here is the query I'm trying:
mDb.delete("my_table", "my_id NOT IN ?", new String[]{"(SELECT _id FROM my_table2)"});
Thanks.

You shouldn't use a .rawQuery, as you stated, but you can use .execSQL() to accomplish it. I regularly use it for deletions myself.

I think the only way is to execute your select and dynamically combine your WHERE clause.

You have to use execSQL instead rawQuery because rawQuery is used for the statement that return data and execSQL is used for the statements that don't return data like DELETE

Related

How do I copy a column from one table to another in SQLite Android? [duplicate]

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

Sqlite Android Raw Query INSERT INTO Not Working

I wanna insert some row to a table in Android, I'm using this:
INSERT INTO MyTable (Column_1, Column_1) VALUES ('X',100);
The query runs and no exception is thrown, but when I retrieve all rows from MyTable, no row
is returned.
I do not want to use the insert method, because the queries are read from a file and I want to insert them to the database.
What's wrong with my code?
Update : The rawQuery() method doesn't run the query, but execSQL() does.
The title of your question suggests you're using rawQuery(). It just compiles the SQL but does not run it. Calling one of the moveTo...() methods on the returned Cursor would also execute the SQL.
For an insert query, use execSQL() instead of rawQuery(), even if the documentation incorrectly states it should not be used with INSERT.
How is your Table organized?
You seem to use twice the same column Column_1 is this a typo?
Alternative, you could use insert-command of SQListeDatabase insteat:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java.lang.String,%20java.lang.String,%20android.content.ContentValues%29

Is 'ORDER BY _id' enough to get rows in insertion order?

I'm using SQLite DB in my Android app.
Now I need to implement data extraction in the same order in which it was inserted.
Is it enough to do SELECT ... ORDER BY _id ... if the _id column is INTEGER PRIMARY KEY AUTOINCREMENT?
Or should I add a column to store the date and time a row has been created?
You can use ORDER BY _id. Make sure you are using a List and not a Set....
No need, an id generated by AUTOINCREMENT is enough.
Yes, if you just need the order, you may use such statement or just simple "SELECT ... FROM sometable". Results will be equals.

difference between rawquery and execSQL in android sqlite database

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

How to return unique rows within a column in Android SQLite?

Really all I want to do is say the query SELECT DISTINCT column FROM table but I can't figure out how to structure it in the enourmous query methods that are part of SQLiteDatabase
I'm just trying to get the names of all contacts in a table.
You seem to know the sql query you want to run. Have you tried using rawQuery()?
You will probably find this version of the SQLiteDatabase#query method most useful.

Categories

Resources