Ormlite Join 3 Tables (Many-to-Many) - android

I'm trying to create search interface via ORMLite in Android. There are 3 tables created, that are making many-to-many relationship. (Recipe >> RecipesForTag << Tag)
tagQueryBuilder.where()
.like(Tag.COLUMN_NAME, "%" + text + "%");
recipesForTagQueryBuilder
.joinOr(tagQueryBuilder);
recipeQueryBuilder
.joinOr(recipesForTagQueryBuilder)
...other joins
.where()
.like(Recipe.COLUMN_TITLE, "%" + text + "%");
PreparedQuery<Recipe> preparedQuery = recipeQueryBuilder.prepare();
return recipeDao.query(preparedQuery);
When I'm not using line with .joinOr(recipesForTagQueryBuilder) everything is working fine. So what am I doing wrong?

OK my bad.
The only thing that was bad was using joinOr while I needed leftJoinOr... The question has one main problem: when middle table is empty no result will be find (also when ...other joins is nonempty). So result is simple:
recipesForTagQueryBuilder
.leftJoinOr(tagQueryBuilder);
recipeQueryBuilder
.leftJoinOr(recipesForTagQueryBuilder)

Related

Sorting Query for SQL

How can we achieve the following sorting order using a query in SQL?
Title_a
Title_b
Title_c
Title_d
Title_1a
Title_1b
Title_1c
Title_1d
Title_11a
Title_11b
Title_11c
Title_111a
Title_111b
Title_111c
Title_111d
Title_1111a
Title_1112a
Title_1112b
Title_12a
Title_12b
Title_12c
Title_1311a
Title_1311b
Title_1311c
Title_1311d
I have tried String sort="CAST ("+ "title" + " AS INTEGER)";
but the results are not satisfying
For those strings it would order fine if it weren't for that last character.
So for those strings you could order by what's before the last character, then the last character.
select *
from yourtable
order by substr(title,1,length(title)-1), substr(title,length(title),1)
Expanding on my comment, "Title_1311b" the value 1311 is added to the ASCII value of "b".
Declare #col varchar(15) = 'Title_1311b'
SELECT SUBSTRING(#col, PATINDEX('%[0-9]%', #col), PATINDEX('%[0-9][^0-9]%', #col) - PATINDEX('%[0-9]%', #col) + 1) + ascii(right(#col,1))+1
Can you add this to your sql query and see what happens?
ORDER BY SUBSTRING(#col, PATINDEX('%[0-9]%', #col), PATINDEX('%[0-9][^0-9]%', #col) - PATINDEX('%[0-9]%', #col) + 1) + ascii(right(#col,1))+1

Sqlite Android order by & group_concat

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

Count query with large amount of objects

I am using parse.com to populate a listview in android. Each item in the list view has a textview that shows the like count and another one that shows the comment count.
Now, according to parse.com
"For classes with over 1000 objects, count operations are limited by timeouts. They may routinely yield timeout errors or return results that are only approximately correct. Thus, it is preferable to architect your application to avoid this sort of count operation."
what would be the recommended/ideal way of going about it then?
What I did is I created a column called commentCount, and a column called likeCount. Then in afterSave, I modified the appropriate cell.
Parse.Cloud.afterSave("Activity", function(request) {
Parse.Cloud.useMasterKey(); //bypasses ACL requirements
//After commenting, increment commentCount
if(request.object.get("type") == "comment"){
query = new Parse.Query("Posts");
query.get(request.object.get("post").id, {
success: function(post) {
post.increment("commentCount", 1);
post.save();
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
}
});

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.

SQLite insert statement (using execSql) not functioning on Honeycomb or later

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?

Categories

Resources