SQL query does not work on earlier Android versions - android

I have the following SQL query in my app. It works without any issues on my Nexus 7, but crashes with a message about the column "title" not existing on earlier versions of Android.
SELECT feeds._id AS _id, feeds.title, feeds.description, feeds.image,
feeds.groupName, count(items._id) AS itemCount, sum(case when items.read = 0
then 1 end) as unreadCount FROM feeds LEFT JOIN items ON (items.feedId = feeds._id)
GROUP BY feeds._id ORDER BY feeds.groupName
Is this a known issue, or is there something wrong with my query?

Here is a complete sqlite tutorial. Be carefull about your database version. Change it first for every table structure change.

Related

SQLite Group By results order behaviour change 3.28.0 onwards

I have noticed an issue with the order of results using "GROUP BY" with later versions of SQLite that am struggling to solve. I first noticed it when Android 11 came out as it uses SQLite 3.28.0. I have now pinned it down to a behaviour change in SQLite itself.
The issue is the the order of results is changed depending on the version. I'm not sure if it's a bug or intended behaviour or an error on my part (albeit an error that has been working fine for 8 years with older SQLite versions).
Example table:
id|year|command |code
1 |2005|TV |A
2 |2005|TV-CD |B
3 |2005|CD |B
4 |2010|TV |B
5 |2015|TV |C
If I run the following command
SELECT * FROM myTable GROUP BY command ORDER BY _id
With in SQLite 3.22 (Android 10) or 3.23.1 I get:
2|2005|TV-CD|B
3|2005|CD |B
5|2015|TV |C
Which is what I want...
If I run the same command in SQLite 3.28 (Android 11) or higher I get
1|2005|TV |A
2|2005|TV-CD|B
3|2005|CD |B
Here's a quick sample of the table if you want to try it for yourself
CREATE TABLE 'myTable' ('_id' integer PRIMARY KEY ,'year' NUMERIC ,'command' TEXT, 'code' TEXT);
INSERT INTO myTable VALUES ("1","2005","TV","A");
INSERT INTO myTable VALUES ("2","2005","TV-CD","B");
INSERT INTO myTable VALUES ("3","2005","CD","B");
INSERT INTO myTable VALUES ("4","2010","TV","B");
INSERT INTO myTable VALUES ("5","2015","TV","C");
SELECT * FROM myTable GROUP BY command ORDER BY _id
https://www.jdoodle.com/execute-sql-online/ was useful for testing as it allows you to change the SQLite version on the fly.
As I already mentioned in my comment, the result of a query like:
SELECT * FROM myTable GROUP BY command
is unpredictable, because GROUP BY should be used only for aggregation, something like:
SELECT command, MAX(year) AS max_year
FROM myTable
GROUP BY command
which will return the max year for each command.
So if the behavior of GROUP BY with SELECT * ... has changed in newer versionsshould not be a problem if you did not use such queries.
From what I read in your question, you are expecting in the results for each command the row with the max id.
In standard sql and without window functions, which are still not supported in most versions of android SQLite, you could aggregate first and then join to the table.
But, SQLite has a documented feature to use so that you can use bare columns. This is valid:
SELECT MAX(_id) AS _id, year, command, code
FROM myTable
GROUP BY command
ORDER BY _id
You will get the row with the max _id for each command.
I've found a solution and can confirm it works in both versions of SQLite
SELECT _id,MAX (year),command,code FROM myTable GROUP BY command ORDER BY _id

WITH RECURSIVE query with android room

Need to get rows with last 10 recent dates from table, even if some of the dates are missing from table.
This query is working fine to get dates but the problem is same query in android room dao throwing compile error at 'SELECT x+1' x cannot be resolved.
#Query(WITH RECURSIVE
stepDtailsTable(x) AS (
SELECT 0
UNION ALL
SELECT x + 1 FROM stepDtailsTable
LIMIT(SELECT((julianday('2019-03-20')-julianday('2019-03-01')))+1)
)
SELECT date(julianday('2019-03-01'),'+'||x||' days')as date FROM stepDtailsTable")
StepDetailsPojo getLastTenDayData();
I have noticed that you are using constants for the julian dates whereas you wanted to query the last 10 recent dates from a table. Here is a query that does that, with filling gaps from the maximum date found in the table to 10 days earlier. I cannot test on your platform so it may not resolve the compiler error you are getting, maybe because RECURSIVE is not supported. Hopefully it gets you a step further.
WITH RECURSIVE
cte(x) AS (
SELECT julianday(date(max(thedate))) from TheDataTable
UNION ALL
SELECT x-1 FROM cte
LIMIT 10
)
SELECT date(x) FROM cte

Sql Lite Query will return 0 records on Android Lollipop

So I understand the version of sql on lollipop changed (http://www.sqlite.org/releaselog/3_8_0.html) , I studied that release list but still cannot figure out why my query fails. Its a simply query really:
select * from Lists where Name LIKE 'test' AND _Group LIKE ''
I'm on Android 5.1.
*What is the sample data that you think matches this search criteria?*
Usually "LIKE" contains a wildcard,
e.g. LIKE "test%".
In your case, instead of the two LIKE you can use "=".
Here is a good example of using LIKE SQLite query, 'LIKE'

SQLite UPDATE value with row number possible/options?

Hi i'm working on an SQLite viewer in android using java and shell commands (so commands have to be one line) i've written the layout and viewer and all everything is perfect. I set it up so when a value is long pressed an edit text shows where the user can input the new value and then when okay is pressed it should update the value.
I have the column name, old and new value, database name, table name etc however the issue is allowing them to update the value i've seen things like the where clause but the issue is if a column has the value multiple times (which could very well be the case) it won't know the correct row.
So bear in mind i'm new to sqlite been working with it less than a week. Is there a way i can update the column value with the columns name and row number?
What are my options to update the table.
I also don't really under stand this say i have a table like this (one column)
sample
--------
0
0
0
1
0
1
1
0
How would you update the third row to equal 1? If I did
sqlite3 DATABASEHERE "UPDATE TABLEHERE SET sample='1' WHERE sample='0'"
The where statement describes rows 1, 2, 3, 5, and 8 so there has to be a way to use row number?
Thanks for any help
SQLite gives you access to rowid. So you can write SELECT rowid, col FROM table1 and then use it to update the table :UPDATE TABLEHERE SET sample='1' WHERE rowid=3

Delete last row in a table android

My question is quite straightforward. I have a table, with, lets say x rows, with each an id, but I don't know how many rows I have. Now I want to delete the last row of my table for some reason... Is there an easy way to do that in android? I have been trying to use the last_insert_rowid in my where clause...but no luck so far...
Any idea to do that with the sqlite database tools of android?
I assume that last_insert_rowid means that you are talking about db.
DELETE FROM test WHERE id = (SELECT MAX(id) FROM test);

Categories

Resources