How to delete specific field using Objectify? - android

I'm trying to delete specific field from my DB table. But unfortunately unsuccessfully. I tried this code:
Query<Movie> query = ofy().load().type(Movie.class).filter("name =", "movie name");
QueryResultIterator<Movie> queryIterator = query.iterator();
while (queryIterator.hasNext()){
Movie m = queryIterator.next();
if(p.getYear()!= null){
ofy().delete().entity(p.getYear()).now();
}
}
I also tried this:
Movie m = ofy().load().type(Movie.class).id("movie id").now();
List<Long> actors = m.getActor();
ofy().delete().entity(actors).now();
But it also didn't work.
What did i miss?

You need to set the property to delete to null and then save the entity. A delete will delete the whole entity, not any single property.
Example:
Movie m = ofy().load().type(Movie.class).id("movie id").now();
m.actors = null;
ofy().save().entity(m).now();

Related

Get SQL insert statement string from model?

Pretty simple question, assuming I've got the model for a row in a table, I'd like to get the insert statement necessary to create that row.
List<MyModel> updatedRows = new Select()
.from(MyDatabase.getMyModels().get(table))
.where(Condition.column(NameAlias.builder("id").build())
.in(new Select(UpdatedRecord$Table.updated_record_id)
.from(UpdatedRecord.class)
.where(UpdatedRecord$Table.updated_table.eq(table))))
.queryList();
StringBuilder updateStatements = new StringBuilder();
for (MyModel tableModel : updatedRows) {
// this is the insert statement, but there's no way to get it as a string
tableModel.getModelAdapter().getInsertStatement();
updateStatements.append(insertSqlStatementString);
}
tableModel.getModelAdapter().getInsertStatement() properly returns the insert statement, however, it is in the form of a DatabaseStatement, and I can't find any documentation for that class. I want the insert statement as a string.
Looking at that line in the debugger shows that underneath it there is a Statement and inside of that, mSQL which holds the string.
Can anyone help me out or point me in the right direction?
Tried this to no avail:
String tableCreateStatement = tableModel.getModelAdapter().getCreationQuery();
DatabaseStatement dbSt = tableModel.getModelAdapter().getCompiledStatement();
tableModel.getModelAdapter().bindToInsertStatement(dbSt, tableModel);
AndroidDatabaseStatement a = (AndroidDatabaseStatement)dbSt;
String p = a.getStatement().toString();
Here's what I ended up using after the main contributor to DBFlow responded to my question here:
// BaseModel row;
ContentValues values = new ContentValues();
row.getModelAdapter().bindToInsertValues(values, row);
String query = SQLite.insert(row.getClass()).columnValues(values).getQuery();

Realm Android merge two queries

I have a query, for my custom object which I form like this:
Path path = paths.get(i);
RealmList<RealmInt> ids = path.getPlaces();
query = realm.where(Place.class);
if(ids.size()==0){
} else {
int j = 0;
for (RealmInt id:ids){
if(j++>0){
query = query.or();
}
query = query.equalTo("id",id.getVal());
}
}
It's from some post at StackOverflow. Now, I want to add .contains to that query but it does not seem to work, it returns items like .contains is not there. Any idea ?
I found a solution, the key was to use beginGroup() and endGroup()

Clear Sugar database table column records after every start

I have a listview with some name and url.. After my app starts I check if there is any record in my local database First clear my old records then fill table columns with listview data and if there isn't just insert data... this is how I'm doing it :
List<LocalProduct> allAuthors = LocalProduct.listAll(LocalProduct.class);
if (allAuthors == null) {
for (int allList=0;allList<adapter.getCount();allList++){
Product my = adapter.getItem(allList);
String offline_name = my.name.toString().trim();
String offline_url = my.image_url.toString().trim();
LocalProduct book = new LocalProduct(offline_name, offline_url);
book.save();
}
}else {
allAuthors.clear(); //I tried this
allAuthors.removeAll(allAuthors); //And this
for (int allList=0;allList<adapter.getCount();allList++){
Product my = adapter.getItem(allList);
String offline_name = my.name.toString().trim();
String offline_url = my.image_url.toString().trim();
LocalProduct book = new LocalProduct(offline_name, offline_url);
book.save();
}
}
But my table records are still storing same records after every start ..
What is the correct way !?
Insert this line before intialize LocalProduct class list:
SugarRecord.deleteAll(LocalProduct.class);
Example:
SugarRecord.deleteAll(LocalProduct.class);
List<LocalProduct> allAuthors = LocalProduct.listAll(LocalProduct.class);
clear() and removeAll() only remove item from List (which is in memory when loaded) not remove record in database.
If you want to clear record in database you need to use :
db.delete(TABLE_NAME, null, null);

Create or update record in ActiveAndroid ORM

I'm using active android as orm in my android project.
after I get a json response from server, I wanna create a field if that item does not exist, or update it if that item already exists.
Record existence is determined via a field named slug.
How can I achieve this via ActiveAndroid? since I don't see how to achieve in the wiki.
Assume your class name is YOUR_CLASS which has a slug field. Then do the following with ActiveAndroid:
YOUR_CLASS item = new Select()
.from(YOUR_CLASS.class)
.where(slug = ?,_slug_value_)
.executeSingle();
if(item == null) {
item = new YOUR_CLASS();
item.slug = _slug_value_;
}
//change what you want then save the item.
item._field_to_change_ = _new_value_;
item.save();

Saving multiple data one at a time in database in Android

I want to save multiple files to my database one by one.
And what happen here using my codes is this:
What I want to happen is like this one:
here is my code:
//Arraylist for getting the multiple brand code
ArrayList<String> content = new ArrayList<String>();
for (int j=0; j<checkSelected.length; j++) {
if(checkSelected[j]==true) {
String values = BrandListAdapter.mListItems.get(j);
//content.add(values);
Cursor rSubBrand = databaseHandler.getReport_SubBrandCode(values);
String SubBrandCode = rSubBrand.getString(rSubBrand.getColumnIndex(Constants.SUBBRAND_CODE));
content.add(SubBrandCode);
//Casting and conversion for SubBrand Code
String subBrand = content.toString();
//SAVE SUBBRAND
databaseHandler.SaveSubBrand(new Cons_iReport (ReportCode, subBrand));
Toast.makeText(getApplicationContext(), subBrand, Toast.LENGTH_LONG).show();
}
}
Mistakes:
content.add(SubBrandCode);
do you know how to remove the '[ ]' in the saved data? (e.g. [AC001]) All I want to save is the AC001 only.
Solutions:
Call clear() method of ArrayList before adding new values into it.
Its giving you [AC001] as you are subBrand by doing content.toString();. Don't convert it to string, instead use content.getString(position) and it will give you String value.

Categories

Resources