Azure Mobile Services Android Offline Example - "createdAt","updatedAt" columns empty - android

i have added in the ToDoItem table of the local DB of the mobile device the columns "createdAt" and "updatedAt", because they also exist in the Azure Database ToDoItem table. Those columns are never updated in the localstorage table although i refresh the list (the other table columns are updated normally).
Why could this happen?
The example i followed is here: http://azure.microsoft.com/blog/2014/08/07/offline-support-in-azure-mobile-services-android-sdk/
Thank you

Without having seen any of your code I assume you haven't decorated the CreatedAt and UpdatedAt properties with the corresponding annotations. Make sure your properties look like this and it should work:
[Microsoft.WindowsAzure.MobileServices.CreatedAt]
public DateTimeOffset? CreatedAt { get; set; }
[Microsoft.WindowsAzure.MobileServices.UpdatedAt]
public DateTimeOffset? UpdatedAt { get; set; }
Ps. you don't have to add these columns of your aren't using them.

Related

Update an item column in a table related to a field in an other table (2 tables case) android room database

Simple grocery app on Android. Everything is local using Room DataBase, using MVVM model and Kotlin. I have a DataBase, a Repository and a ViewModel. I am practicing with this app and I am not comfortable with DataBase presently.
I have two tables:
ShoppingItems (Items I want)
ReferenceItems (Items I usually buy and know the price)
In the ReferenceItems table I have 3 columns:
Id
Name (unique)
Unit Price
In the ShoppingItems table I have the following columns:
Id (PrimaryKey)
Name
Amount
Reference Id (Foreign Key) (if the id is in the Reference table else I got an Id from a default reference item I pre populated in the table)
Unit Price ( = corresponding reference item Unit Price)
Now, everything works perfectly using LiveData<> to read information such as quantity or total price.
My question is: Can I use only the ReferenceItems Id in the ShoppingItems table to get the Unit Price.
In fact, the user will be able to edit the unit price in the ReferenceItems table if needed. I would like the app to update only ONE field, in ReferenceItems table. In the actual situation I will have to update the price in ReferenceItems table AND ShoppingItems table.
Can I use only the ReferenceItems Id in the ShoppingItems table to get the Unit Price
Yes, you can.
Scenario 1 (without auxiliary class).
Change you query in dao to something like this:
#Query("select shoppingItems.*, referenceItems.unitPrice from shoppingItems left join referenceItems on shoppingItems.referenceId=referenceItems.id")
LiveData<List<ShoppingItems>> getShoppingItems()
But to achieve that - you have to keep you "unitPrice" field in your "shoppingItems" class (of course, you can leave it empty while inserting data). You can play with #Ignore annotation trying not persist this field the the db, but I can't guarantee that query would return the price in that case.
Scenario 2 (with auxiliary class).
Needed steps:
Add new auxiliary class ShoppingItemsWithPrice (this class should not persist in DB, so it has no #Entity).
public class ShoppingItemsWithPrice {
#Embedded
public ShoppingItems shoppingItems;
#Relation(parentColumn = "referenceId", entityColumn = "id")
public ReferenceItems referenceItems;
}
And you DAO query:
#Query("select * from shoppingItems")
LiveData<List<ShoppingItemsWithPrice>> getShoppingItemsWithPrice()
As a result, query returns you object with all you have in both tables. You can optimise that (to get just one needed field) according to this article

Migrating auto generate primary key

How can I migrate a primary key field, which was not set to Auto generate before?
From
#PrimaryKey
private int id;
To
#PrimaryKey(autoGenerate=true)
private int id;
Since Sqlite does not support altering columns, my only guess is to migrate the whole table as is and resetting the constraints.
Do I even have to migrate the database during the development process or can I just rebuild it, since my database will change rapidly, so I don't have to migrate every time?
I suggest you to change your approach: add an unique identifier (UID) as alternative way to identify records.
You can define a UID with annotation Entity on your POJO.
#Entity(indices={#Index(value="uid", unique=true)})
publi class Pojo {
..
public String uid;
..
}
When you insert a record in your database, you can define uid field using:
String uuid = UUID.randomUUID().toString();
You can use the UUID field to identify your records, in absolute way. When you migrate to a version to another, you don't work with the old ids, you can always work with UID.

Cupboard for SQL-Lite to set unique field on one column

I am using Cupboard for android project. the project was very simple but later I need to set some fields as unique column in my class.
eg:
class book {
Long _id;
String name; // this name should be unique
String auther;
}
I am not able to figure out how to do that.
Issue has been resolved recently by Cupboard:
https://bitbucket.org/littlerobots/cupboard/issues/8/flexible-way-to-control-how-entities-are

Update all columns in ormlite database table in Android

I have the following database table object:
public class Goal {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField
private String goal_title;
#DatabaseField
private String goal_desc;
#DatabaseField
private String goal_why;
...
}
I have added some rows to this table and now I want to write a query to update all the columns of a row in this table. I have seen documentation of ORM and could not get an idea how to write this query. Please help me how to write this query.
I have added some rows to this table and now I want to write a query to update all the columns of a row in this table.
I think you need to RTFM. I've spent a long time on the ORMLite documentation and I think it covers the UpdateBuilder pretty well. Feel free to ask more specific questions and I can add more details if not.
To quote from the docs:
The DAO can also be used to construct custom UPDATE and DELETE statements. Update statements are used to change certain fields in rows from the table that match the WHERE pattern - or update all rows if no where(). Delete statements are used to delete rows from the table that match the WHERE pattern - or delete all rows if no where().
To tweak the example code to use your Goal object:
UpdateBuilder<Goal, Integer> updateBuilder = goalDao.updateBuilder();
// update the goal_title and goal_why fields
updateBuilder.updateColumnValue("goal_title", "some other title");
updateBuilder.updateColumnValue("goal_why", "some other why");
// but only update the rows where the description is some value
updateBuilder.where().eq("goal_desc", "unknown description");
// actually perform the update
updateBuilder.update();
Hope this helps.

Android ORMLite, use pre-populated database and use it

I have a pre-populated database, I hadd .csv and make a database in sqllite manager and imported all values into this database.
Now I put this database into android's assets folder and want to use this via ORMLite in my android application.
Please, need your help and will be thankful to you.
Now I put this database into android's assets folder and want to use this via ORMLite in my android application.
Boy there is a lot of ground to cover here to use ORMLite with this.
The short answer is that you will need to create Java objects which correspond to your database tables. Each Java object should have fields that match the table columns with the appropriate types with #DatabaseField annotations.
For example, if you CSV file was:
# name, id, street
Bill Jones,123,131 Main St.
and your table created is something like:
create table user (name VARCHAR(255), integer id, street VARCHAR(255));
The Java object you will need is something like:
public class User {
#DatabaseField(id = true)
int id;
#DatabaseField
String name;
#DatabaseField
String street;
}
Then you would use ORMLite to read in objects from your database. You should see the ORMLite home page and the Getting Started section of the documentation. For linking up with the existing database, you should read the section of the manual about using with Android.
Any additional questions I'd ask to the ORMLite Users Mailing List.

Categories

Resources