I visited sqlfiddle.com and made some tests.
I wanted to get a SQL statement that would let me insert a row, and have a field (below is called active) whose value depends on the table being empty or not.
I used the following schema:
CREATE TABLE kids (
identifier INTEGER PRIMARY KEY,
name VARCHAR,
surname VARCHAR,
active INTEGER);
INSERT INTO
kids
VALUES
(1, "name1", "surname1", CASE WHEN EXISTS (SELECT * FROM kids) THEN 0 ELSE 1 END);
INSERT INTO
kids
VALUES
(2, "name2", "surname2", CASE WHEN EXISTS (SELECT * FROM kids) THEN 0 ELSE 1 END);
And I get the following result when performing a SELECT * FROM kids:
+---+-------+----------+---+
| 1 | name1 | surname1 | 1 |
+---+-------+----------+---+
| 2 | name2 | surname2 | 0 |
+---+-------+----------+---+
This is perfect. The thing is, how do I perform this insert in Android using SQLite and ContentProviders?. Both insert (on ContentProviders and SQLite instance take a ContentValues as a parameter, and I can't add a subquery there, it would be literally read and written.
Is it even possible to achieve this ? If you can think of any other approach, it is also welcome.
Thank you.
Another way of achieving your requirement would be to check the value of underlying field before insertion and then insert the entire data along with computed value in active field
ContentValues ensures that it contents are never interpreted as SQL, so this is not possible.
Unless you actually need to provide data to other apps, do not use content providers. (And if you need to give data to other apps, it would be a bad idea to allow them to execute arbitrary SQL.)
Related
If table Scores looks like this:
_id | score
---------
1 | 1,000
2 | 2,000
3 | 3,000
4 | 4,000
5 | -1
6 | -1
7 | -1
Will the following query always return the rows in _id ascending order?
SELECT * FROM Scores
Also, will the following query always return the first ordered occurrence of _id (that is, 5)?
SELECT _id FROM Scores WHERE Score = -1 LIMIT 0, 1
The key here is ALWAYS. I have tested it and it works as intended but I want to verify this outcome is guaranteed and that an order by clause is not needed in these cases.
Thank you all.
By default, the rows are logically stored in order of increasing rowid.
And if your select stament does not include an order by, or the table has no index, the default sorting order will always be the primary key column, which is the _ID column in this case
Read more here
One of the principles of the databases is that you can not suppose that your registers are ordered in any way, as the internal implementation of SQLite may differ between devices. You should use the operator order by to assure a certain order.
In my application I have a tree structured table and I want to know that if I can fetch the tree structure data from sqlite database by simply applying a single query. Currently I am lopping it manually using a for loop and able to meet my requirement but I am not statisfied with that approach as its more complex, may be time consuming, etc.
My table table structure is as below,
parent_id | cat_id
null | 1
1 | 2
1 | 3
1 | 4
4 | 5
4 | 6
So, now above is my table structure and I want to fetch the tree structure like,
If I pass parent_id as 1 in my query then it should return 2,3,4,5,6 as 1 is parent of 1, 2, 3, 4 and 4 is parent of 5,6 and also 4 is child of 1
So, input and output expected by me,
input output
1 2,3,4,5,6
4 5, 6
So, it there a simple way to acheive the above tree structure by using sqlite query?
OR
There is no way to acheive this using sqlite query and I need to follow the current method or way that I am using it by manually looping all the category ids.
You can use group_concat method as it is available in sqlite.
For your question i have created one table called Trans and added parent_id and cat_id columns.
select parent_id, group_concat(cat_id) from trans group by parent_id;
I have table like this i have mentioned below.
and i got resultant like this :
Hope this will help you.
I didn't find any solution from sqlite itself, so created an Algorithm that creates Tree structure by iterating over each id & over its sub_ids and write into database.
I had gone through Closure Tables which gives the expected result that I was looking as a solution
Some more references of Closure Table -
http://technobytz.com/closure_table_store_hierarchical_data.html
http://karwin.blogspot.in/2010/03/rendering-trees-with-closure-tables.html
I have a table orders, consisting of 3 columns:
order_id int primary key,
cust_id integer,
order_date integer
with data:
order_id | cust_id | order_date
1 | 10 | 1325376000
2 | 10 | 1325548800
3 | 10 | 1325894400
4 | 11 | 1325462400
5 | 11 | 1325721600
6 | 12 | 1325721600
7 | 12 | 1326326400
I'm trying to write a query to give a Cursor containing the most recent order for a given customer that I can then pass to a SimpleCursorAdapter and bind to a ListView, such that the user sees the following:
10 1325894400 (formatted as human readable date)
11 1325721600
12 1326326400
I've tried joining the table to itself in various ways without any luck:
http://sqlfiddle.com/#!2/77b22d/1/0
If I have to populate an ArrayList and use an ArrayAdapter I will, but I'd like to exhaust this option first. Thanks!
EDIT: Apologize for the differences between here and the SQLFiddle, brain running on two separate threads. The Fiddle is the 'correct' data set.
2nd EDIT: Added a new wrinkle (ignore table above, see the SQL fiddle). Adding a field for free-form text and then running the query returns the first record in the GROUP BY, plus the field for the max_date. I need to pull the whole record containing the date that equals max_date. Adding a WHERE clause breaks the query. Thoughts?
Try this
select
order_number
, cust_number
, order_date
from orders o1
where order_number =
(
select order_number
from orders o2
where o2.cust_number = o1.cust_number
and order_date =
(
select max(order_date)
from orders o3
where o3.cust_number = o2.cust_number
)
)
This will get you the correct records and you can format the date as you like in the main query.
Note: My answer is a bit different form your display since the example here and the Fiddle are different. used the Fiddle one
create table orders (order_number integer primary key,
cust_number integer not null,
order_date integer not null);
insert into orders values (1001,10,1005),
(1,10,1325376000),
(2,10,1325548800),
(3,11,1325894400),
(4,11,1325462400),
(5,11,1325721600),
(6,12,1325721600),
(7,12,1326326400),
(8,12,1326326460);
If you just want the latest record for each customer, I think this will work:
SELECT order_number, cust_number, max(order_date) as max_date FROM orders GROUP BY cust_number
The values you put on the link are different from the ones you posted here but you are looking for:
select o1.cust_number, max(o1.order_date)
from orders o1
group by o1.cust_number
order by o1.cust_number ASC
This will give you for each customer the most recent order.
:I have this table:
id | name | chapter | book
1 hamlet I Hamlet
2 Ismael IV Moby Dick
The behaviour I expect for new values is:
Same name && same book => Character exists so Same id than the existing one:
2 Ismael X Moby Dick
Same name && different book => Character doesn't exist so Changes the id:
3 Ismael XX The Bible
My question is:
Do I have to make the query before insert new values and then insert the new value?
or there is a way to do it in a automatic way by setting up a trigger or something?
My CREATE TABLE statement
db.execSQL("CREATE TABLE characters (id INTEGER NOT NULL ,
name TEXT NOT NULL , chapter TEXT NOT NULL , book TEXT NOT NULL );");
I think the most elegant way of doing so would be for you to isolate the properties of your rows which permit to identify them UNIQUELY. Lets say for instance that name + book make the line unique, then you could concatenate these strings into one identifier in your object, add a column to your database, put and index on it and search on this index when you want to insert new elements.
I have a database that can have similar rows, but have different keys and a different boolean column. Here is what the database looks like:
columns: _id, name, img, address, available
Two entries can look like this:
_id | name | img | address | available
-------------------------------------------------------
1 | John | http://img.com/222 | 1 Main St | 1
2 | John | http://img.com/222 | 1 Main St | 0
I want a query that will give me all results that have a distinct key, and if there are duplicate entries(ignoring the fact that _id would be different), it will give back only the first one. Here is the query I have:
SELECT p1.*
FROM (SELECT DISTINCT _id, available FROM people) p
INNER JOIN people p1
ON p1._id=p._id
ORDER BY p1.available DESC;
I know this isn't right, but maybe it explains a little what I am looking for. Would I want to use GROUP BY here?
I want a query that will give me all results that have a distinct key, and if there are duplicate entries(ignoring the fact that _id would be different), it will give back only the first one.....the _id isn't what I want to be distinct, as they [the ids] are already unique. ... . Ideally it will order by 'available' in descending order so that if there are two columns with the same data(aside from _id and available), it will return the row with '1' for the available column
select name, image, address, max(availability) as avail
from T
group by name, image, address
Then you can join the set returned by the query above, as an inline view, to your table:
select * from T
inner join
(
select name, image, address, max(availability) avail
from T
group by name, image, address
) as foo
on T.name = foo.name and T.image = foo.image and T.address = foo.address and T.availability = foo.avail
It would help to have a composite index so: (name, image, address).
Caveat: if there is more than one row where a specific {name, image, address} triad has availablility =1, the query will return multiple rows for the triad:
2 | John | http://img.com/222 | 1 Main St | 1
6 | John | http://img.com/222 | 1 Main St | 1
P.S. It sounds as though you wished the triad (name, image, address) had been created in your table an alternate UNIQUE key.
this sql may solve your problem:
select b.* from (select distinct _id from people) a, people b where a._id = b._id order by b.available
I actually just asked a similar question and received a great answer from an experienced user here:
SQL Populating with distinct data and a sequence
Based on what he told me, perhaps this query would provide you with what you want:
SELECT p1.*
FROM (SELECT DISTINCT _id, name from people) p
INNER JOIN people p1
ON p1._id=p._id
ORDER BY p1.available desc
apologies if that's a fail and doesn't work!
EDIT: It just occurred to me that I have no idea which distinct name+_id combo this will extract.. the available=1 or the available=0 or a random selection..! Let me know what happens anyway..
If you want the first row which has the lowest _id among those that have the highest available value (between 1 and 0), you can "record" the _id inside the aggregated value generated by the grouping.
The value to compare is constructed in a way that orders the record by their available field in descending order and then by their _id field in descending order, and allow to easily retrieve the value of the _id with the modulo operator (assuming available max value is 1 and the ids are never above 100000000).
select people.* from people
inner join (
select name, img, address,
min((1-available)*100000000 + _id) avail_id
from people group by name, img, address
) as foo on people._id = foo.avail_id % 100000000;
I adapted it Tim's query.
You can also do that without subquery:
select people.* from people
left outer join people as other on
other.name = people.name and
other.img = people.img and
people.address=other.address and
(1 - people.available) * 100000000 + people._id >
(1 - other.available) * 100000000 + other._id
where other.available is null;