How to get the last data entry with greenDao? - android

I am using greenDao to manage my database and I need to get the last entry that was made. How do I do this? Is there a way to query the last ID entered or to somehow save it for later use?

this works for me:
daoSession.getMyDao().queryBuilder().limit(1).orderDesc(MyDao.Properties.Id).list()

Related

How to display data stored in SQLite database using List<>

Actually, my problem is that when I had done it with Room the Id which I was entering were coming in a sequence like:100,101 and so on, but on the other hand when I am using SQLite user ids are appearing as they are entered i.e., not in sequence like: 100,101,99,98. Please help me out.

How to structure local database for deleting rows when syncing from server

I currently have a local SQLite database that will update periodically from a server.
Initially, the local database will be empty. The app will fetch entire server database as json, and insert all the new rows locally.
After some time the app will fetch the entire server database as json again, and insert any new rows, or update any existing rows that have changed.
Now, the issue is if the server's database has removed an item (row). The app will never be aware of these removed items, and it will exist forever locally.
What's the best way to handle this?
A few ideas:
Add a "is deleted" flag on each item. This would allow the app to remove any items, but this seems like it would be harder to maintain, and a lot of changes for each table. Although it would allow you to undelete items as well.
Same idea as the flag, just modify as existing field and return it. For example, return a negative id ( id = 150 -> id = -150 ). This wouldn't require any database changes, but is confusing to anyone who doesn't know what's happening.
Modify the result to return a list of "deleted" items. This wouldn't be too much work, but doesn't seem like a good solution either.
Is there any standard pattern to follow for this?
Thanks.
You can use Realm instead of SQLite for better experience, And When the app is fetching the data from server clear all data in the realm and insert new records, It will not save the removed item.
Thank you.

A check before saving to the Cloud Parse

I want to do a check on the cloud first before I save the data. For example...
Do a query to check if a Task name and User situated to it is there.
If there is update it.
If not save to the cloud.
I'm having the issue with checking with two params like you could with SQLite on Android.
Any Ideas?
Thank you
Just call whereEqualTo multiple times to add more constraints to the query.

Adding latest record at the top of Azure Sql table

Iam following this tutorial :- http://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-android-get-started/
to use mobile services with Azure.
Currently when I add items to the table, the data gets stored randomly. I want it to store the items in a such a manner that the latest record comes at the top of the list.
How can I do it?
Thanks
From the looks of that tutorial, the table in the database which has been created is using a UniqueIdentifier (equivalent to a GUID in code) as the Id. (presumably PK).
GUIDs are generated "randomly" and don't sort nicely in chronologically created order. It's not that the data is getting stored randomly. The Guid is randomly generated, and the table/index stores it in exactly the correct place based on it's Id value.
At the end of the day, for such a small amount of information, the order it's stored in the table isn't really important so long as you can retrieve it in the order you want.
From the looks of the demo, the data table also has a _createdAt column on each data entity. Have a look in the TodoItem & TodoAdapter code to find where it actually executes the query against the data store. It should be quite easy to add an OrderBy to that call to force the returned order to sort by _createdAt

Keeping a DB updated - Android

With your help, I have successfully created a web page which has its own MySQL DB and then uses a JSON web service to pass the values my Android application.
The next stage is to copy them into a local SQLite DB so the phone does not require an internet connection to view the list.
I have successfully implemented code that creates a table, and inserts values into this table.
The problem is, I don't want it to necessarily re-write the whole DB at a time, which is the current idea to keep it current.
I might have things in the DB deleted or more things added, and want the local DB to reflect this.
What steps can I take to delete things that are no longer present in the MySQL DB in the SQLite one, and add new things?
Cheers
The best way to do this should be to have a date time column in your database and then just fetch the posts that are newer then the last time you fetched from the database, in your android app you could save an date time in your preferences
I'm sorry I missed that you wanted to delete items as well, than this probably isn't the best approach. Are you using the mySQL only for the API? Then you could probably mark posts as deleted inserted odd feeling them.
You can think this approach, parse your JSON response into objects of your data model and then create the content values holding that data model to work with the database in your device.
Once you have the content values, use a content resolver to work with your database and try an update of each data parsed from the JSON response (each content value), update method of the content resolver will return an integer value that tell us the number of rows updated in the database, if such number is 0 means that no data were in the database, so make an insertion, simply.
In that way you first search if the current data is present in the database, if it is, update it, if not, insert it.

Categories

Resources