I have created a view in sqlite using sqlite manager.
Create View vMeterReading
as
SELECT M._id as Meter_id, M.MeterNumber, R1.ReadingDate, R1.Reading AS CurrentReading, R2.ReadingDate AS PrevReadingDate, R2.Reading AS PrevMeterReading, R2.Rate as Rate, R2._id,R1.TenantMeter_id
FROM (Meters AS M INNER JOIN TenantMeters ON M._id = TenantMeters.Meter_id) INNER JOIN (MeterReading AS R1 INNER JOIN MeterReading AS R2 ON R1.TenantMeter_id = R2.TenantMeter_id) ON TenantMeters._id = R1.TenantMeter_id
WHERE (((R2.ReadingDate)=(SELECT Max(R3.ReadingDate)
FROM [MeterReading] AS R3
WHERE (R3.TenantMeter_id = R1.TenantMeter_id)
AND (R3.ReadingDate < R1.ReadingDate)
))) OR (((R2.TenantMeter_id) Is Null))
ORDER BY M.MeterNumber, R1.ReadingDate.
I have tried to use this view (select * from vMeterReading) in android and I get the error as titled.
I have also connected to the sqlite db from command prompt and tried to execute the same query. I get the same error.
What is wrong with the sql statement?
It works well in sql manager(firefox add-on).
SQLite makes no guarantees whether a table name prefix (like R1.1) is part of the result column name, or not.
You should explicitly give a name to all the view's columns with AS.
The problem was that the SQLite version used by android is not the same as the version used by sQLite manager. The android version does not support the join i was using.
The query below executes with out problems.
Got this advice from this site http://sqlite.1065341.n5.nabble.com/
SELECT M._id as Meter_id, M.MeterNumber, R1.ReadingDate as ReadingDate,
R1.Reading AS CurrentReading, R2.ReadingDate AS PrevReadingDate, R2.Reading
AS PrevMeterReading, R2.Rate as Rate, R2._id as _id,R1.TenantMeter_id as TenantMeterID
FROM Meters AS M INNER JOIN TenantMeters ON M._id = TenantMeters.Meter_id
INNER JOIN MeterReading AS R1 ON R1.TenantMeter_id = TenantMeters ._id
INNER JOIN MeterReading AS R2 ON
R1.TenantMeter_id = R2.TenantMeter_id
WHERE (((R2.ReadingDate)=(SELECT Max(R3.ReadingDate)
FROM [MeterReading] AS R3
WHERE (R3.TenantMeter_id = R1.TenantMeter_id)
AND (R3.ReadingDate < R1.ReadingDate)
))) OR (((R2.TenantMeter_id) Is Null))
Related
I am trying to create a trigger like below in sqlite android. It works fine in other sqlite engine but shows syntax error in android. Please help me to make this update statement work.
create trigger if not exists trigger_cascade_update_carry_forward
after update on cf_details
begin
update cf_details set cf = c_f from (with recursive dates_after(d) as
( values(new.date) union
select strftime('%Y-%m',d||'-01','+1 month') as next_month
from dates_after where exists(select 1 from cf_details where date = next_month and account_id = new.account_id))
select d,c_f from dates_after,(select cf+net as c_f from cf_details where account_id = new.account_id and date = strftime('%Y-%m',(select d from dates_after order by d limit 1)||'-01','-1 month'))) where date = d;
end;
I'm using the sqlite3 : 3.8.2 with sqliteman (Ububuntu 14.04) to try the query that I need in my program Android.
Android version 4.0+.
I have 3 table to use to obtain data
I found this problem:
when I use this query in sqliteman
SELECT st.nome
AS nome,
st.cognome
AS cognome,
(SELECT ae.usercode
AS usercode
FROM assenze ae
WHERE st.ultimo_evento = ae.fk_evento)
AS ultimo_evento,
(SELECT
GROUP_CONCAT(
ev.nome_evento, ", "
)
AS nome_evento
FROM eventi ev
WHERE ev.studente_id = st.id
)
AS nome_evento
FROM studenti st
WHERE st.classe_ident ='4297'
ORDER BY st.cognome
the output is:
EP, USC, R1, R, A ... perfect for me
but when I use it in android with db.rawQuery() the result is
A, EP, R, R1, USC ... no good for me
also seems to be the same result of this query (execute in sqliteman and android):
SELECT
st.nome AS nome,
st.cognome AS cognome,
ae.usercode AS ultimo_evento,
GROUP_CONCAT(
ev.nome_evento, ", "
)
AS nome_evento
FROM studenti st
LEFT JOIN eventi ev
ON st.id = ev.studente_id
LEFT JOIN assenze ae
ON st.ultimo_evento = ae.fk_evento
WHERE st.classe_ident ='4297'
GROUP BY st.id
ORDER BY st.cognome
Probably it's my fault but I'm still trying to find a solution...
Thanks
seems that it works also without ORDER BY ev.data_ora_evento. But in android still not working...
The group_concat() documentation says:
The order of the concatenated elements is arbitrary.
However, you could try to force an order by executing group_concat() on a subquery that is sorted:
SELECT GROUP_CONCAT(ev.nome_evento, ", ") AS nome_evento
FROM (SELECT nome_evento
FROM eventi
WHERE studente_id = st.id
ORDER BY whatever) AS ev
im getting data from database which match any prefex how to add % on bothside of variable? below is my query i want to add '"+School_name+"' to this '%"+School_name+"%' how to do that?
Cursor mCursor3 = db.selectQuery("SELECT * FROM uss_school sch LEFT JOIN uss_school_to_level sl ON sch.school_id = sl.school_id LEFT JOIN uss_level l ON sl.level_id = l.level_id WHERE sch.name like '"+School_name+"'");
i have select query as follows
SELECT DISTINCT tt.firstname,
tt.lastname,
tc.caseid,
tt.courtcode AS courtid,
tcou.courtname,
(SELECT COUNT(*)
FROM tblcasetrafficticketlink
WHERE caseid = tc.caseid) AS ticketcount,
Max(tt.violationdate) AS violationdate,
( tt.address1
|| ','
|| tt.address2 ) AS address,
tt.city,
tt.state,
tt.zip,
tt.dob,
tt.sex
FROM tblcase tc
LEFT OUTER JOIN tblcasetrafficticketlink tcttl
ON tc.caseid = tcttl.caseid
LEFT OUTER JOIN tbltraffictickets tt
ON tcttl.courtid = tt.courtcode
AND tt.ticketnumber = tcttl.ticketnumber
AND ( tcttl.ticketextension = tt.ticketnumberex
OR tt.ticketnumberex IS NULL )
LEFT OUTER JOIN tblcourts tcou
ON tcou.courtid = tt.courtcode
WHERE tc.casetype = 'TRAFFIC'
AND tc.caseid<='"+recent_min_caseID+"'
GROUP BY tc.caseid,
tt.firstname,
tt.lastname,
tt.dob,
tt.sex,
tt.courtcode,
tcou.courtname,
tt.city,
tt.state,
tt.zip,
tc.casestatus,
tt.address1,
tt.address2
ORDER BY tc.caseid DESC
LIMIT 100;
this is taking much time to get data. can anybody help to improve performance.Here PRAGMA is useful? if so how? if not, tell me the way to fix this issue.
I've found that SQLite on Android seems to have some... unexpected quirks. In my case, it turned out that doing a straight query like select * from Foo where Bar is null was MUCH slower than selecting only the IDs and then fetching each row by ID individually. YMMV.
I am using execSql to process an INSERT statement, and it has worked without error on all pre-Honeycomb versions of Android. In Honeycomb and later, the application just hangs. It does not return an Exception, or any kind of error.
The INSERT statement uses a compound SELECT statement with 3 UNION's to provide the values.
Has anyone else encountered this?
Edit: It seems it is only the final SELECT statement that causes the hang.
insert into RESULTS (Int_ID, SubjID, SubjName, SubjCompID, SubjCompName, ObjID, ObjName, ObjCompID, ObjCompName, IntType, MechID, Direction, Effect, Strength, Comment, Sort1, Sort2 )
SELECT Int_ID, subdc.ID_comp as SubjID, subdc.Name_comp as SubjName, ID_subject as SubjCompID, subcompd.Name as SubjCompName, objdc.ID_Compound as ObjID, objdc.Name_Compound as ObjName, ID_object as ObjCompID, objcompd.Name as ObjCompName, IntType, MechID, Direction, Effect,Strength,Comment, (subdc.ID_compound + objdc.ID_Compound)as Sort1, (ID_subject + ID_object)as Sort2
FROM Int
INNER JOIN t_Components subdc ON ID_subject = subdc.ID_Component
INNER JOIN t_Components objdc ON ID_object = objdc.ID_Component
INNER JOIN Comps subcompd ON ID_subject = subcompd.DrugID
INNER JOIN Comps objcompd ON ID_object = objcompd.DrugID
WHERE subdc.ID_compound <> objdc.ID_Compound
Just needed to add more statements to the WHERE clause;
Maybe this post should be removed?