Android sqlite INSERT multiple rows with same constant - android

I'm trying to execute the following sqlite command to copy some values in a table:
INSERT OR IGNORE INTO table (column1, column2) VALUES((SELECT DISTINCT column1 FROM table WHERE column2 = '2'), 1);
What I want is for the 1 to be used for all the SELECTed rows, but it's only used once. How do I fix that?
I was thinking using a temp table with the default value to what I want. I know temp tables are used for complex queries, but it feels like this isn't that complicated and there could be an easier solution to this.
Example:
I want the following table...
column1 column2
1 2
4 2
5 3
4 4
...to become the following table...
column1 column2
1 2
4 2
5 3
4 4
1 1
4 1

Looks like you want to put the fixed column2 value in the select:
INSERT OR IGNORE INTO table (column1, column2) SELECT DISTINCT column1, 1 FROM table WHERE column2 = '2';
Also note the absence of VALUES.

Related

SQL Query, How to find the first and last value in the given table need by sequential order with conditions

I have ids in my table, ids start from 1 to 20, I want a query, to find the first and last records in a given table but I want the result by some condition.
For example: if I have the record
1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
This is the island part of the classic gaps and islands problem (With the gaps part being finding the missing values in between each island). If you search for that term, you'll find a ton of material about how to calculate them.
One approach (Requires Sqlite 3.25 or newer for window function support):
sqlite> CREATE TABLE ex(id INTEGER PRIMARY KEY);
sqlite> INSERT INTO ex VALUES (1),(2),(3),(4),(5),(9),(10),(11),(12),(13),(19),(20);
sqlite> WITH cte AS (SELECT id, id - row_number() OVER (ORDER BY id) AS grp FROM ex)
...> SELECT min(id) AS rangestart, max(id) AS rangeend FROM cte GROUP BY grp;
rangestart rangeend
---------- ----------
1 5
9 13
19 20
SQL Query to find first record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> ASC LIMIT 1
SQL Query to find last record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> DESC LIMIT 1
For example: if I have the record 1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
If you need result like you have mentioned, then you can set LIMIT in your query to get how many records you can have in that query.
QUERY:
SELECT * FROM <table_name> LIMIT <any_number>

Android: SQLite update Single value in a col. and then all other of the same col

I have the below table in my DB
id name is_current
1 apple 0
2 banana 1
3 mango 0
4 grapes 1
5 pineapple 1
I want to execute an update query which will update the (fruit) table last column (is_current) single value and at the same time the whole column values as well. For example the first row has an apple with id=1 and I want to set this value to 1 and all other fruit values to zero so the table look like,
id name is_current
1 apple 1
2 banana 0
3 mango 0
4 grapes 0
5 pineapple 0
Currently I am using two different queries and to different methods to achieve this
1st: is to set all values of is_current column to 0
String sql = "UPDATE "+TABLE_NAME +" SET " + is_current + " = '"+ Zero +"';
2nd: is to set the apple values to 1 by using id
String sql = "UPDATE "+TABLE_NAME +" SET " + is_current + " = '"+ One +"' WHERE "+ id + " = "+rowId;
So how can I combine these two queries to a single one to achieve this?
If you want to do this stuff for a specific fruit/ID (e.g. every time you update apples) you can set a trigger such that when you update that fruit, then it will automatically set to zero all other rows.
However if you want to do this stuff in a more general way then you need to perform 2 queries (as told by #Der Golem)
how can I combine these two queries to a single one to achieve this?
You can't.
You have to execute at least 2 commands.
A command to update all the is_current values to 0.
And a command to update the specified record to 1.

Insert or replace 2 unique values

I'm using sqlite in android app and I want to make insert in table by 2 unique row values. For example:
id| column1 | column2 | column3|
1. 1 2 3
2. 2 3 4
Unique columns - COLUMN1 and COLUMN2
INSERT OR REPLACE INTO HISTORY (id, column1, column2, column3) VALUES (null, 2, 3, 6);
But this query shouldn't works 'cause 1st and 2nd values isn't unique in table
Thnx.
If I understand your question, simply use the UNIQUE constraint when building your table:
CREATE TABLE History (
_id INTEGER PRIMARY KEY,
column1 INTEGER,
column2 INTEGER,
column3 INTEGER,
UNIQUE (column1, column2));
Then if you try these INSERT statements:
INSERT INTO History (column1, column2, column3) VALUES (2, 3, 0);
INSERT INTO History (column1, column2, column3) VALUES (2, 3, 6);
The second will fail because column1 and column2 already exist.
Understand that if you use INSERT OR REPLACE in my example, the second statement will replace the first (no exception thrown).
but how can i add unique to existed table?
Typically you would use ALTER TABLE to add a constraint to your existing tables, but SQLite does not allow this... However you can workaround this limitation:
CREATE UNIQUE INDEX name ON History(column1, column2);

Android Database usage

This may be a simple question as I have not done any database work for a while!
I have two tables with data like the below
Table 1
Rows with Primary Keys 1,2
Table 2
Rows with Foreign keys 1,2,3,4
I was to be able to perform a DELETE statement which will remove all rows from Table 2 that do not have a corresponding primary key in table 1, which in this case would result in only rows with foreign keys 1 & 2 being left in the table.
I should mention that this is on Android so I am using SQLite and also I am interested in the ease of doing this via a content provider.
Thanks for any help
Try this:
String SQL="DELETE FROM Table2
WHERE (Table2.FQ1,Table2.FQ2) NOT IN (SELECT PK1,PK2 FROM Table1)";
db.SQL(SQL);
But i'm not sure that the (Table2.FQ1,Table2.FQ2) sentence will run into the NOT IN

SQL sqlite conditional query in sqlite (for Android)

Hello I have a table with that structure:
ID VALUE
1 3
2 5
3 12
if I query select id,value,value+5 from table. Thats the result
ID VALUE NEW_VALUE
1 3 8
2 5 10
3 12 17
And what I want to make a query indicating the id and the new value that return the whole table but with a 3rd column indicating the new values after inserting. for example for myQuery(id=2,value=8)
ID VALUE NEW_VALUE
1 3 3
2 5 8
3 12 12
Is posible to do that in the same query?
YOu can use the WHERE clause to select only the rows you want ("...if the student has the given id..."):
update T
set col3 = col2 + 5
where id = 2
Of course, col3 would have to exist before you can update it. So you will either have to issue an ALTER-TABLE statement (if your implementation supports it) or recreate the table with the desired columns, import the original data (INSERT INTO YOURNEWTABLE...SELECT ... from YOUROLDTABLE) and then update col3.
If you don't want to "persist" this third column but only need it to be displayed when you query:
select id, col2, col2 + 5 as myComputedValue
from T
where id = 2
Finally, if you want to display all rows but change the addend conditionally (add zero to col2 when the id is not one of the ones you desire but add 5 when it is) then you can use the CASE statement.

Categories

Resources