How to update column with ORMLite - android

I have an already created database for Android application and I'm using ORMLite for query to SQLLite.
I have added a column in category and I want to insert data in that column.
I have row, e.g.
catId=5 catname="food" catType=?
I want to update catType on the basis of catId. How can I update this catType column with the catId in ORMLite.
I am using this approach:
Dao<Category, Integer> catDao = getHelper().getCategoryDao();
QueryBuilder<Category, Integer> queryBuilder = catDao.queryBuilder();
queryBuilder.where().eq("categoryId", 5);
PreparedQuery<Category> pq = queryBuilder.prepare();
Category category = catDao.queryForFirst(pq);
category.setCategoryType("BarType");
catDao.createOrUpdate(category);
Please provide me a better solution.

I want to update catType on the basis of catId. How can I update this catType column with the catId in ORMLite.
ORMLite also supports an UpdateBuilder. Here's how you could use it:
UpdateBuilder<Category, Integer> updateBuilder = catDao.updateBuilder();
// set the criteria like you would a QueryBuilder
updateBuilder.where().eq("categoryId", 5);
// update the value of your field(s)
updateBuilder.updateColumnValue("catType" /* column */, "BarType" /* value */);
updateBuilder.update();
See the UpdateBuilder docs for more examples.

Dao<Category, Integer> catDao = getHelper().getCategoryDao();
List <Category> categoryList = catDao.queryForAll();
for (Category c: categoryList ) {
c.setCategoryType("BarType");
catDao.update(c);
}

Related

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);

Multiple Query Condition in Android using ORMLITE

i want to make a simple query, with multiple conditions
I use OrmLite to map entity object.
Now I want to search for an object into my table.
Supposing i have a Person entity that maps PERSON table, what I want to do is to initialize an object with some parameters and search it.
Suppose a function searchPerson(Person oPerson)
If i pass an object OPerson like this
Id = null
Name = John
Age = null
Sex = male
Is possible to write a query to reach that goal? Something like this pseudo-code
pers = (from p in db.Table<Person>()
where (if OPerson.Id !=null) p.Id==OPerson.Id}
AND {(if OPerson.Name !=null) p.Name.Contains(OPerson.Name)}
AND {(if condition) where-contion}
select p).ToList();
I know that i can do multiple query in this way
list=PersonDao.queryBuilder().where().eq("name",OPerson.name)
.and().eq("sex",OPerson.sex").query();
but I want also to check if the value exists
where (if OPerson.Id !=null) p.Id==OPerson.Id}
#ArghArgh is close but doesn't have the ANDs right. The problem is that the AND statements are conditional on whether there were any previous statements. I'd do something like:
QueryBuilder<Person, Integer> queryBuilder = dao.queryBuilder();
Where<Person, Integer> where = queryBuilder.where();
int condCount = 0;
if (oPerson.id != null) {
where.eq("id", oPerson.id);
condCount++;
}
if (oPerson.name != null) {
where.like("name", "%" + oPerson.name + "%");
condCount++;
}
...
// if we've added any conditions then and them all together
if (condCount > 0) {
where.and(condCount);
}
// do the query
List<Persion> personList = queryBuilder.query();
This makes use of the where.and(int) method which takes a number of clauses on the stack and puts them together with ANDs between.
I think that you must use the QueryBuilder.
Try something like this
QueryBuilder<Person, Integer> queryBuilder = PersonDao.queryBuilder();
// get the WHERE object to build our query
Where<Person, String> where = queryBuilder.where();
if(oPerson.Name!=null)
where.like("Name", "%"+oPerson.Name+"%");
// and
where.and();
if(Person.Sex!=null)
where.like("Sex", "%"+oPerson.sex+"%");
PreparedQuery<Person> preparedQuery = queryBuilder.prepare();
Than you can call it in this way
List<Person> list = PersontDao.query(preparedQuery);

Ormlite orderBy max(ColumnName)

Hi I need to use order by max(columnName) in ORMLite. I have the SQL query but I need to know how this query is used. This is my query:
SELECT * FROM table where place = 'somePlace' group by name
order by MAX (statusDate)
statusDate column contains date in "yyyy-dd-mm" format. The result I got is the list with recentDates.
Use a query builder, and function where and orderBy to preoceed
QueryBuilder<YourObject, Integer> q = yourDaoObject.queryBuilder();
Where<YourObject, Integer> wh = q.where();
wh.eq("place", "some_place");
q.orderBy("statusDate", false);
List<YourListOfObects> yourList = q.query();
But before that you should store a long instead to store your Date https://stackoverflow.com/a/6993420/2122876
i got same names with different dates and i need only the recent date.
If you are trying to get element from Table with the maximum statusDate then you should be doing an descending order-by with a limit of 1. Something like:
QueryBuilder<Foo, Integer> qb = fooDao.queryBuilder();
qb.where().eq("place", "some_place");
qb.orderBy("sttusDate", false); // descending sort
// get the top one result
qb.limit(1);
Foo result = qb.queryForFirst();
I did something like this. Please create your own query builder on the first line.
QueryBuilder<MyRowObject, Integer> queryBuiler = "Get Query builder" //getDaoXXX().queryBuilder();
MyRowObject firstLatestRow = queryBuiler.orderBy("dateColoumn", false).queryForFirst();
Hope this helps

ORMLITE order by a column from another table

I would like to get information by a sql like this but in "ORMLITE"
SELECT * FROM tableA a INNER JOIN tableB b on a.fieldA = b.fieldB
ORDER BY a.fieldZ, b,fieldX;
I try this in ORMLITE:
QueryBuilder<B, Integer> bQuery = bDao.queryBuilder();
bQuery.orderby("fieldX", true);
QueryBuilder<A, String> aQuery = aDao.queryBuilder();
aQuery.orderby("fieldZ", true);
list = (List<T>) aQuery.join(bQuery).query();
But the result is not correct because it is not order by a.fieldZ. How can I do this?
Thank you.
instead :
aQuery.orderby("fieldZ", true);
you should use :
aQuery.orderbyRaw("a.fieldZ, b.fieldX");
it's work for me

ORMLite: Query empty foreign field

I have two entities: EntityA and EntityB. EntityB has a foreign field of EntityA:
#DatabaseField(foreign=true, columnName=ENT_A_NAME)
private EntityA entityA;
Now I want to query all entries of EntityB where EntityA is null. So I've made the following query:
bDao.queryBuilder().where().isNull(EntityB.Ent_A_NAME).prepare();
If I execute the query I get an empty result set back.
If I execute queryAll() I see that the entries of EntityB have always an associated Order-Object with all values set to null/0.
How can I execute my query?
I'm not sure #Toni4780. The following test case works for me. I don't see anything that you are doing wrong.
In the table for EntityB, ORMLite actually stores the id of the EntityA so I am wondering if it is null or 0. Have you tried the following?
bDao.queryBuilder().where().eq(EntityB.Ent_A_NAME, 0).prepare();
or both:
bDao.queryBuilder().where().isNull(EntityB.Ent_A_NAME).
or().eq(EntityB.Ent_A_NAME, 0).prepare();
Here's my unit test code that works:
Dao<Order, Integer> orderDao =
DaoManager.createDao(connectionSource, Order.class);
TableUtils.createTable(connectionSource, Order.class);
int numOrders = 10;
for (int orderC = 0; orderC < numOrders; orderC++) {
Order order = new Order();
order.val = orderC;
assertEquals(1, orderDao.create(order));
}
List<Order> results = orderDao.queryBuilder().where()
.isNull(Order.ACCOUNT_FIELD_NAME).query();
assertNotNull(results);
assertEquals(numOrders, results.size());

Categories

Resources