SQL Query error - what do I do wrong? - android

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.)

Related

Android Studio Sqlite same name only 1 time listing

Trying to learn Android studio. And I expect your help on this.I am adding and listing data with sqlite.
for example;
id - name - value
1 - john - 100
2 - mark - 200
3 - john - 150
4 - john - 200
5 - adam - 400
what I want to do, list only names one time.
1 - john
2 - mark
3 - adam
private void showlist() {
ArrayList<DataListItems> contactList = new ArrayList<DataListItems>();
contactList.clear();
String query = "SELECT * FROM data ";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
DataListItems contactListItems = new DataListItems();
contactListItems.setid(c1.getString(c1
.getColumnIndex("id")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setValue(c1.getString(c1
.getColumnIndex("value")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
DataListAdapter contactListAdapter = new DataListAdapter(
SiteList.this, contactList);
lvCustomList.setAdapter(contactListAdapter);
}
You can use the GROUP BY name to select only one name. However, the id selected would be an arbitrary id for each group (name).
your code could use String query = "SELECT * FROM data GROUP BY name";
If you wanted the first/lowest id per name then you could use min(id) in conjunction with GROUP BY NAME.
your code could use String query = "SELECT min(id) AS id, name, value FROM data GROUP BY name";
You say that your expected result should be
1 - john
2 - mark
3 - adam
That would be more complicated (and perhaps of little use) as the id for adam is 5 not 3 (I assume that is simply a typing error).

SQL syntax error: near “.”?

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)

How to make... SQLite select DISTINCT -> query(true, ...)

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.

How to insert a table in to group in Corona SDK (.Lua)?

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.

select query with join is selecting data slowly from database in android

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.

Categories

Resources