SQL error when using two tables in Android - android

I'm trying to get orders sorted by the customers delivery order.
This SQL statement works in my desktop database, but when I run it on the Android I get an error.
I tried using _id as well.
near "c": syntax error: , while compiling: SELECT * FROM Orders o,
Customer c WHERE o.Route='My Route' c.id=o.Customernum ORDER BY
c.StopNum

Your SQL statement is missing an AND:
SELECT * FROM Orders o, Customer c WHERE o.Route='My Route' AND
c.id=o.Customernum ORDER BY c.StopNum

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

Error in studio SQLiteException: json_each

I have json Array in my column and i want to extract json array as separate values I am using sqlite query
SELECT
json_extract(t2.value, '$.time') as time
FROM
json_each((SELECT column_name
FROM tbl_name)) AS t2
My column looks like this
[{"time":0,"value":0},{"time":2,"value":0},{"time":0,"value":0}]
It works fine in sqlite browser but I get an error in Android studio.
SQLiteException: no such table: json_each (code 1):
Update: I am using room database and it works fine for other queries
json_each and json_extract would need JSON1 extension. It's not available on Android. You need to do your json mapping in code.

"WITH" clause Query not working in android

this is my rawQuery
WITH authorRating(aname, rating) AS SELECT aname, AVG(quantity) FROM book GROUP BY aname
error is in Logcate
android.database.sqlite.SQLiteException: near "WITH": syntax error (code 1):
WITH is supported since SQLite 3.8.3, and that is not shipped with all Android versions.
Anyway, this query is not a valid WITH clause, and lacks the actual query.

How to select from static list of values in android sqlite, syntax error near values(

I am trying to create a static list of timestamps so that i can join them agains another table to create chart data. So far I have a query in this format SELECT * FROM (VALUES('a'),('b'),('c'),('d')) AS tbl ,which is working in sqlitestudio but not in android 4.4. When I run the query in the phone I get the error
android.database.sqlite.SQLiteException: near "VALUES": syntax error (code 1): , while compiling: SELECT * ...
I have also tried wrapping the values term inside another select like this SELECT * FROM (SELECT * FROM (VALUES('a'),('b'),('c'),('d'))) AS tbl but I still get the same error.
The full query now looks like this
SELECT * FROM (select * from ( VALUES (1458111312025),
(1455667200000),
(1455753600000),
(1455840000000),
(1455926400000),
(1456012800000),
(1456099200000),
(1456185600000),
(1456272000000),
(1456358400000),
(1456444800000),
(1456531200000),
(1456617600000),
(1456704000000),
(1456790400000),
(1456876800000),
(1456963200000),
(1457049600000),
(1457136000000),
(1457222400000),
(1457308800000),
(1457395200000),
(1457481600000),
(1457568000000),
(1457654400000),
(1457740800000),
(1457827200000),
(1457913600000),
(1458000000000),
(1458086400000))) i LEFT JOIN (
SELECT (osysdate- (osysdate % 86400000) ) interval, SUM(field004) totalval FROM onlineactivities WHERE field003 !=300000 and osysdate> 1455605711999 GROUP BY interval )
onl ON i.'' = onl.interval;;
Note that this works in sqlitestudio with sqlite version 3.8.10 but not in android kitkat (not sure about the sqlite version in it) What could be the problem?
Also please check out this question which is what I am trying to do but with sqlite and this answer
The VALUES syntax is available since SQLite 3.8.3, which is not available in every Android version.
To be compatible with earlier SQLite versions, you have to use a compound query:
SELECT 1458111312025 UNION ALL
SELECT 1455667200000 UNION ALL
...
Alternatively, put all the values into a temporary table.

Why do I get syntax error when creating a view with column names?

Developing an Android app, but this is a SQLite question.... My SQL is much ropier than my Java.
This is the SQL (copied from a log file, in code it's constructed from various constants):
CREATE VIEW albums (_id, name , type ) AS SELECT rowno, name, subtype FROM metadata WHERE subtype = 'album'
but it throws:
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling:
Specifying column names in the view seems to me to be clearly permitted, if I understand the flow chart in the sqlite documentation here, and at this time of night I really can't see what's wrong. I have tried changing the name of the _id column in case it was something to do with the initial underscore, but to no avail and in any case I will need the _id column later for the CursorAdapter that will end up using this View.
At the bottom of the documentation page you linked it states:
Note that the column-name list syntax is only supported in SQLite
versions 3.9.0 and later.
Version 3.9.0 is not supported by Android. Lollipop has 3.7.11. Marshmallow has 3.8.10.2.

Categories

Resources