When I try to run this query:
UPDATE Line_Master
SET Line_Master.line_count = t2.task_count FROM (SELECT COUNT (am.line_id) AS task_count, lm.lineId
FROM Assets_Master am,Line_Master lm,Section_Master sm,Task_Point_Master tpm,Task_Master TM
WHERE (lm.line_id = am.line_id)
AND (am.asset_id = sm.asset_id)
AND (sm.section_id = tpm.section_id)
AND (tm.task_point_id = tpm.task_point_id)
GROUP BY lm.line_id) t2
WHERE Line_Master.line_id = t2.lineId
I get this error:
Internal Error
syntax error: near ".":Syntax error
I don't know what you are trying to do, but this way the Syntax is correct. But i don't know if it shows your desired result and if sqlite supports this...
UPDATE Line_Master
SET Line_Master.line_count = (SELECT t2.task_count FROM (SELECT COUNT (am.line_id) AS task_count, lm.lineId
FROM Assets_Master am,Line_Master lm,Section_Master sm,Task_Point_Master tpm,Task_Master TM
WHERE (lm.line_id = am.line_id)
AND (am.asset_id = sm.asset_id)
AND (sm.section_id = tpm.section_id)
AND (tm.task_point_id = tpm.task_point_id)
GROUP BY lm.line_id) t2
WHERE Line_Master.line_id = t2.lineId)
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;
This is the structure and data of my table funco inside database your.db (SQLite):
Id Disp Tp
1 Ball 2
2 Light 1
3 Sea 4
This is my code:
var db = SQLiteDatabase.openDatabase(this.filesDir.path +
"/your.db",null, SQLiteDatabase.OPEN_READWRITE)
val c = db.rawQuery("Select Id, Disp, Tp From Funco Where Id<=2;",null)
var stat = c.moveToFirst()
var result=""
while (stat) {
val mId = c.getInt(c.getColumnIndex("Id"))
val mDisp = c.getString(c.getColumnIndex("Disp"))
val mTp = c.getInt(c.getColumnIndex("Tp"))
result += "$mId $mDisp $mTp | "
stat = c.moveToNext()
}
c.close()
db.close()
The result value:
1 Ball 2 | 2 Light 1 |
If I replace my second line for
val c = db.rawQuery("Select ?, ?, ? From Funco Where ?<=2;"
,arrayOf("Id","Disp","Tp","Id"))
There is no error raised, but the cursor is empty!
Why?
Update:
The #tynn answer is right. I think that the documentation is subtle.
I think that compiler shoud be throw an error and not just return empty cursor.
In the same flavor, one can write
val c = db.query("funco",arrayOf("Id","Disp","Tp"),"Id<=?",
arrayOf("2"),null,null,null)
But below code fails
val c = db.query("funco",arrayOf("Id","Disp","Tp"),"?<=?",
arrayOf("id","2"),null,null,null)
As the documentation states:
selectionArgs String: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
The question marks are there to bind different values to a precompiled SQL query. This functionality is not suitable for configuring the columns to be used in the query. Your first approach is the correct one. You could make the 2 in it dynamic though:
db.rawQuery("Select Id, Disp, Tp From Funco Where Id<=?;", arrayOf("2"))
My query is:
String _query = "select DISTINCT Y.CATE_CODE4 CATE_CODE, Y.CATE_NAME4 CATE_NAME,
0 IS_PRESET from G_MASTER X INNER JOIN CATEGORY_VIEW Y ON X.CATE_4 = Y.CATE_CODE4
where X.IS_SALE = 1 AND Y.IS_LOCKED4 = 0 AND Y.CATE_NAME2 not in ('XXXX')";
How does Android SQLite make db.query()?
First of all you should read SQLiteDatabase reference.
You could use a rawQuery(), here's an example of it :
rawQuery - db.rawQuery("select DISTINCT Y.CATE_CODE4 CATE_CODE, Y.CATE_NAME4 CATE_NAME,
0 IS_PRESETG_MASTER X INNER JOIN CATEGORY_VIEW Y ON X.CATE_4 = Y.CATE_CODE4 from table where X.IS_SALE = 1 AND Y.IS_LOCKED4 = 0 AND Y.CATE_NAME2 not in ('XXXX')"",new String[]{"data"});
It's just an example, you have to adapt your code to this.
I get error message when i try to insert a table into a group
My table code is containing images
Here is the code i am using for the table
local myJoints = {}
for i = 1,5 do
local link = {}
for j = 1,17 do
link[j] = display.newImage( "link.png" )
link[j].x = 121 + (i*34)
link[j].y = 55 + (j*17)
physics.addBody( link[j], { density=2.0, friction=0, bounce=0 } )
-- Create joints between links
if (j > 1) then
prevLink = link[j-1] -- each link is joined with the one above it
else
prevLink = wall -- top link is joined to overhanging beam
end
myJoints[#myJoints + 1] = physics.newJoint( "pivot", prevLink, link[j], 121 + (i*34), 46 + (j*17) )
end
end
and here is the code for group
GUI:insert(myJoints);
i have my background image in the GUI group and it is covering the table.
I don't know if it is actually possible to insert table into a group
Any help please
Thanks in Advance!
You can't insert a table into a group using the "insert" method because that method is looking for a display object. Try calling GUI.myJoints = myJoints. Also keep in mind that your table just references your display objects, which is different from having them in a group.
Here is SQL UPDATE I'm trying to execute:
UPDATE T
SET T.CurrentStopNumber = TS.CurrentStopNumber
FROM Trip T
INNER JOIN (SELECT TripId, MIN(StopNumber) CurrentStopNumber
FROM TripStop
WHERE TripId = '106504'
AND (IsPickup = 1 OR IsDrop = 1)
AND StopNumber > (SELECT COALESCE(max(StopNumber), 0)
FROM TripUpdate
WHERE TripId = '106504'
AND Type = 2)) TS ON T.TripId = TS.TripId
I get error on second line:
/* Error message: SQL script is wrong mismatched input . expecting "EQ" */
I am familiar with SQL Server and I'm sure it would run on SQL Server just fine. Subquery runs fine and returns 1 row as I expect. I just need to update table with that value. What is wrong?
Use:
UPDATE TRIP
SET currentstopnumber = (SELECT MIN(ts.stopnumber)
FROM TRIPSTOP ts
WHERE ts.tripid = TRIP.tripid
AND ts.tripid = '106504'
AND 1 IN (ts.ispickup, ts.isdrop)
AND ts.stopnumber > (SELECT COALESCE(MAX(tu.stopnumber), 0)
FROM TRIPUPDATE tu
WHERE tu.tripid = ts.tripid
AND tu.type = 2)
GROUP BY ts.tripid)
WHERE EXISTS (SELECT NULL
FROM TRIPSTOP ts
WHERE ts.tripid = TRIP.tripid
AND ts.tripid = '106504'
AND 1 IN (ts.ispickup, ts.isdrop)
AND ts.stopnumber > (SELECT COALESCE(MAX(tu.stopnumber), 0)
FROM TRIPUPDATE tu
WHERE tu.tripid = ts.tripid
AND tu.type = 2))
The JOIN is preferable, but the syntax isn't widely supported.
I don't think that you should specify the table for the fields that you set, as the database already knows which table to update:
SET CurrentStopNumber = TS.CurrentStopNumber
(IIRC, that is not allowed in SQL Server either.)