I am trying to execute the android SQLite query (alternative SQL query) for my project. When I execute the alternative query, an error is generated. How can I solve this problem? Any help will be appreciated.
Table Structure
TABLE (
id VARCHAR(250) NULL
,lastUpdateDate VARCHAR(500) NULL
,createdTime VARCHAR(500) NULL
,takenDate LONG NULL
,endDate LONG NULL
)
DECLARE @SelecctedDate DATE ='2022-11-30'
SQL Query
SELECT *, dateadd(s, convert(bigint, takenDate) / 1000, convert(datetime, '1970-1-1 00:00:00')) AS startdate,
dateadd(s, convert(bigint, endDate) / 1000, convert(datetime, '1970-1-1 00:00:00')) AS enddate FROM @Table
WHERE (
((@SelecctedDate>= dateadd(s, convert(bigint, takenDate) / 1000, convert(datetime, '1970-1-1 00:00:00')) AND takenDate != '0' )
AND (@SelecctedDate <= dateadd(s, convert(bigint, endDate) / 1000, convert(datetime, '1970-1-1 00:00:00')) AND endDate != '0'))
OR ((@SelecctedDate= dateadd(s, convert(bigint, takenDate) / 1000, convert(datetime, '1970-1-1 00:00:00')) AND takenDate != '0' )AND endDate='0')
OR (takenDate ='0' AND (@SelecctedDate = dateadd(s, convert(bigint, endDate) / 1000, convert(datetime, '1970-1-1 00:00:00')) AND endDate != '0'))
OR (takenDate ='0' AND endDate='0' AND (@SelecctedDate = dateadd(s, convert(bigint, createdTime) / 1000, convert(datetime, '1970-1-1 00:00:00')) ))
OR @SelecctedDate IS NULL
)
Alternate of SQL Query (Android SQLITE SQLite)
SELECT * FROM $table
WHERE (
(($selectedDate >= strftime('%Y-%m-%d', takenDate/1000, 'unixepoch') AND takenDate != '0')
AND ($selectedDate <= strftime('%Y-%m-%d', endDate/1000, 'unixepoch') AND endDate != '0'))
OR (($selectedDate = strftime('%Y-%m-%d', takenDate/1000, 'unixepoch') AND takenDate != '0') AND endDate='0')
OR (takenDate ='0' AND ($selectedDate = strftime('%Y-%m-%d', endDate/1000, 'unixepoch') AND endDate != '0'))
OR (takenDate ='0' AND endDate='0' ($selectedDate = strftime('%Y-%m-%d', lastUpdateDate/1000, 'unixepoch' )))
)
When I run SQLite raw query I got an issue:
database.SQLiteException: near "(": syntax error: , while compiling: SELECT * FROM medication WHERE ((( 2022-11-29 >= strftime('%Y-%m-%d', takenDate/1000.0, 'unixepoch') AND takenDate != '0') AND ( 2022-11-29 <= strftime('%Y-%m-%d', endDate/1000, 'unixepoch') AND endDate != '0')) OR (( 2022-11-29 = strftime('%Y-%m-%d', takenDate/1000, 'unixepoch') AND takenDate != '0') AND endDate='0') OR (takenDate ='0' AND ( 2022-11-29 = strftime('%Y-%m-%d', endDate/1000, 'unixepoch') AND endDate != '0')) OR (takenDate ='0' AND endDate='0' ( 2022-11-29 = strftime('%Y-%m-%d', lastUpdateDate/1000, 'unixepoch'))))
CodePudding user response:
Missing operator after OR (takenDate ='0' AND endDate='0'
, perhaps AND
:
SELECT * FROM $table
WHERE (
(($selectedDate >= strftime('%Y-%m-%d', takenDate/1000, 'unixepoch') AND takenDate != '0')
AND ($selectedDate <= strftime('%Y-%m-%d', endDate/1000, 'unixepoch') AND endDate != '0'))
OR (($selectedDate = strftime('%Y-%m-%d', takenDate/1000, 'unixepoch') AND takenDate != '0') AND endDate='0')
OR (takenDate ='0' AND ($selectedDate = strftime('%Y-%m-%d', endDate/1000, 'unixepoch') AND endDate != '0'))
OR (takenDate ='0' AND endDate='0' AND ($selectedDate = strftime('%Y-%m-%d', lastUpdateDate/1000, 'unixepoch' )))
)