Convert SQL query to Android Azure Mobile Service query - android

I have a query that works fine in an Azure SQL database, but I'm trying to use it within my Android application and I can't get it to work correctly. It is to find any overlaps in time for bookings. Here is the SQL query with some dummy data, arrival and depart are both integers:
Select * from bourguestMob.tableObjectBookings
where tabObjID = 28
and day = 30
and month = 3
and year = 2015
and ((arrival <= 1600 and depart > 1600)
or (arrival< 1800 and depart >= 1800));
Using the Mobile Service provided, I have to use the query as something like:
tableObjectBookingsTable.where().field("tabObjID").eq(tables.get(i).getId())
.and().field("day").eq(day).and().field("month").eq(month).and().field("year").eq(year)
.and().field("arrival").lt(intTime).or().field("arrival").eq(intTime).and().field("depart").gt(intTime)
That's what I have partially but I think some part of the query needs to be nested to be evaluated correctly.

I think what you want is to change the end of your query to look like so:
.and(field("arrival").lt(intTime).or()
.field("arrival").eq(intTime).and().field("depart").gt(intTime))
It helps me to look at the oData string to check if you get it right, take a look here for some samples of complex queries:
https://github.com/Azure/azure-mobile-services/blob/master/sdk/android/test/sdk.testapp.tests/src/com/microsoft/windowsazure/mobileservices/sdk/testapp/test/MobileServiceQueryTests.java#L724

Related

Android Firebase query with range (Above, Mid, Bottom)

I just start using Firebase in Android and I wrote query like this.
messageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
.child(conversationID).limitToLast(INITIAL_MESSAGE_COUNT)
.orderByChild("timestamp").startAt(participant.joiningDate).endAt(endAt);
I can only give limitToFirst, limitToLast, startAt, endAt. (I think there is no center)
I need to give timestamp (e.g 10 am sharp) and i wanna query 10 message before 10 am and 10 message after 10 am. (but i don't know when it start, when it end).
Edit
What I want to do is something like this.
messageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message").
child(conversationID).
orderByChild("timestamp").equalTo(endAt).limitToFirst(INITIAL_MESSAGE_COUNT).limitToLast(INITIAL_MESSAGE_COUNT);
But if I do, it crash like this.
java.lang.IllegalArgumentException: Can't call limitToLast on query with previously set limit!
How shall I do?
My data in firebase is something like this.
You can select a range by combining startAt and endAt, both of which operate on the child you order on. For example if you want messages between 10am and 10pm on a certain date, you'd do:
long timestampOf10am = ...;
long timestampOf10pm = ...;
messageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
.child(conversationID)
.orderByChild("timestamp").startAt(timestampOf10am).endAt(timestampOf10pm)
.limitToLast(INITIAL_MESSAGE_COUNT);
Update: If you need 10 items before 10am and 10 items after 10am, you'll need to fire two queries and merge the results client-side.
long timestampOf10am = ...;
beforeMessageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
.child(conversationID)
.orderByChild("timestamp").startAt(timestampOf10am)
.limitToFirst(INITIAL_MESSAGE_COUNT);
afterMessageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
.child(conversationID)
.orderByChild("timestamp").endAt(timestampOf10am)
.limitToLast(INITIAL_MESSAGE_COUNT);

Need help in Firebase query for this scenario

I have DB structure as
and my code query is
quesRef = myRef.child("questions").orderByChild("timestamp").endAt("120");
Ideally as per my understanding all the values whose timestamp is less than 120 should get return, but in my case, almost all the questions are getting returned.
Am I doing something wrong ?
You are querying to an ending value of the string "120" instead of the number 120.
Instead, it should read:
quesRef = myRef.child("questions").orderByChild("timestamp").endAt(120);
I'm not sure why the timestamp would be as low as 120 anyway, but perhaps that's just for testing purposes?

SugarORM offset not working

I'm using SugarORM for my Android app. I want to load messages from the SQLite DB but need an offset.
So I load message 5-15 like for pagination.
Unfortunately the offset function doesn't seem to work as expected.
I wrote:
query = Select.from(Message.class)
.where(Condition.prop("MESSAGES").eq(Long.toString(getId())))
.orderBy("timestamp DESC")
.limit("5")
.offset("10");
query.list();
But this only gives me the limit of 5 elements but doesn't use any offset.
Same if I parameterize the query call like this:
List<Message> messages = getAllMessagesQuery.offset("5").list();
Offset isn't working.
Am I doing something wrong? I use the current SugarORM version 1.5
( compile 'com.github.satyan:sugar:1.5')
stating the working offset functionality. So this shouldn't be the issue.
Any ideas how to make this work?
Well, not sure this is the best way to do but at least it's working this way.
If I add the short version to the limit things work fine.
So using the query like this:
List<Message> messages = getAllMessagesQuery.limit("5,10").list();
gives me 10 messages with a 5 message offset.
If that is too dirty, a raw query is also working fine. So you write:
List<Message> messages = Chat.findWithQuery(Message.class, "Select * from Message where Chat = ? ORDER BY timestamp DESC LIMIT 10 OFFSET 5", getId().toString());
Still no idea how to use the offset parameter directly but at least it works this way.

Query where "Number" column value is empty or is greater than X

I am trying to provide a database-type Android for my users, where I'd like to take the following precausion for future compatibility: make certain rows/elements only available "since version X" of the app.
All versions of the app will query for entries and provide their version code in the query.
Now, I can successfuly retrieve rows that are supported in the current version using:
query.whereLessThanOrEqualTo("sinceVersion", getCurrentAppVersionCode(this));
However, this does not account for and retrieve rows that have empty value for "sinceVersion".
You can solve your problem with compound query
ParseQuery<TestParse> queryLessEqual = ParseQuery.getQuery(TestParse.class);
queryLessEqual.whereLessThanOrEqualTo("sinceVersion", getCurrentAppVersionCode(this));
ParseQuery<TestParse> queryDoesNotExists = ParseQuery.getQuery(TestParse.class);
queryDoesNotExists.whereDoesNotExist("sinceVersion");
ParseQuery<TestParse> compoundQuery = ParseQuery.or(Arrays.asList(queryLessEqual, queryDoesNotExists));
List<TestParse> list = compoundQuery.find();
where TestParse is your class from Parse.com

solution for SQLite function for time difference without java code

Hey please pay attention to solve this problem I know this could be possible but I am not getting the way ..
I am using sqlite in android to store and manipulate the field as shown below in the figure
Figure http://s16.postimage.org/idc13pxdh/image.png
first_start_time,last_start_time,last_end_time are datetime field in SQLite database
I need to get the difference of last_start_time and last_end_time as mean of function so that I don't need to do it programatically
last_end_time = strftime('%Y-%m-%d %H:%M:%S','now','localtime')
last_start_time= strftime('%Y-%m-%d %H:%M:%S','2012-01-27 02:34:56')
,i,e if my firsttime is =2012-06-15 14:54:33
and Endtime is =2012-06-15 14:54:40
then time diff would be like this =00:00:14;
i want to have such difference time like last_end_time -last_start_time
i also want to use trigger to do use in sqlite data base in android
at the last I need that all my total_expended_time should get summed up like ...
select sum(total_expended_time) from study_result
.. one thing keep in mind the result should be in datetime stamp so that i can reflect it to my UI
Any suggestion will be appreciated

Categories

Resources