I want to update multiple cell of single row with active android.
some thing like this,
new Update(Question.class).set("UserAnswer ="+answerID,
"UserAnswerType = "+userAnswerType)
.where("QID = ?", questionID).execute();
but that will gives me error. is there any other way to do this? or am i missing something?
Here is my Question.class for reference
#Table(name = "Questions")
public class Question extends Model {
#Expose
#Column(name = "QID")
private Integer ID;
#Expose
#Column(name = "AnswerID")
private Integer AnswerID;
#Expose
#Column(name = "UserAnswer")
private Integer UserAnswer;
#Expose
#Column(name = "UserAnswerType")
private Integer UserAnswerType;
//Getter and setter methods for fields
public Question() {
// You have to call super in each constructor to create the table.
super();
}
}
You can do something like below. I have done it in one of my project and it worked fine.
String updateSet = " UserAnswer = ? ," +
" UserAnswerType = ? ";
new Update(Question.class)
.set(updateSet, answerID, userAnswerType)
.where(" QID = ? ", questionID)
.execute();
Following similar pattern for any number of columns will work. In case if you have Boolean type fields in your table, this issue can help you.
We Can achieve it like this way.
StringBuilder data=new StringBuilder();
if (!TextUtils.isEmpty(name.getText().toString()))
{
data.append("Name = '"+name.getText().toString()+"',");
}
if (!TextUtils.isEmpty(age.getText().toString()))
{
data.append("Age = '"+age.getText().toString()+"',");
}
if (!TextUtils.isEmpty(empCode.getText().toString()))
{
data.append("EmployeeCode = '"+empCode.getText().toString()+"',");
}
if (!TextUtils.isEmpty(mobNum.getText().toString()))
{
data.append("MobileNumber = '"+mobNum.getText().toString()+"',");
}
if (!TextUtils.isEmpty(adress.getText().toString()))
{
data.append("address = '"+adress.getText().toString()+"'");
}
String str=data.toString();
//-------------and update query like this-----------------
new Update(EmpProfile.class).set(str).where("EmployeeCode = ?", empCode)
.execute();
String x="sent = 1,file = "+n.getString("file");
new Update(Messages.class)
.set(x)
.where("id = ?", lid)
.execute();
Related
I have the following database table
#DatabaseTable(tableName = "Order_Details")
public class DB_OrderDetails {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField()
private int order_id_from_order_table;
#DatabaseField()
private String medicine_code;
#DatabaseField()
private String medicine_name;
#DatabaseField()
private String medicine_type;
#DatabaseField()
private int number_of_units_ordered;
#DatabaseField()
private String generic_name;
#DatabaseField()
private String manufacturer_name;
#DatabaseField()
private double ordering_cost;
... }
I am using ormlite for my local db operations. i want to get the result for
SELECT * FROM Order_Details WHERE (medicine_code='xxxx' AND manufacturer_name='yyyy')
and want to get the result returned in
List<DB_OrderDetails>
can anybody suggest me a way to do it?
Use an object of the Where class to build your query.
List<DB_OrderDetails> list;
try {
Dao<DB_OrderDetails, String> dbOrderDetailsStringDao = dbHelper.getOrderDetailsDao();
QueryBuilder<DB_OrderDetails, String> queryBuilder = dbOrderDetailsStringDao.queryBuilder();
Where where = queryBuilder.where();
where.eq("medicine_code", "xxxx");
where.and();
where.eq("manufacturer_name", "yyyy");
PreparedQuery<DB_OrderDetails> preparedQuery = queryBuilder.prepare();
list = dbOrderDetailsStringDao.query(preparedQuery);
} catch (SQLException e) {
e.printStackTrace();
}
I'm using DBflow to query an SQLite database containing several tables. Since my query contains a lot of joins it's kind of difficult to read using the DBflow join :
SQLite.select()
.from(Plant.class).as("p0")
.innerJoin(Definition.class).as("d0")
.on(Plant_Table.definitionId.withTable(NameAlias.builder("p0").build()).eq(Definition_Table.definitionId.withTable(NameAlias.builder("d0").build())))
.innerJoin(Picture.class).as("pi0")
.on(Plant_Table.pictureId.withTable(NameAlias.builder("p0").build()).eq(Picture_Table.pictureid.withTable(NameAlias.builder("pi0").build())))
.innerJoin(SpaceAssociation.class)
.on(Plant_Table.pictureId.withTable(NameAlias.builder("p0").build()).eq(SpaceAssociation_Table.plantId1))
.innerJoin(Plant.class).as("p1")
.on(SpaceAssociation_Table.plantId2.eq(Plant_Table.plantId.withTable(NameAlias.builder("p1").build())))
.innerJoin(Definition.class).as("d1")
.on(Plant_Table.definitionId.withTable(NameAlias.builder("p1").build()).eq(Definition_Table.definitionId.withTable(NameAlias.builder("d1").build())))
.innerJoin(Picture.class).as("pi1")
.on(Plant_Table.pictureId.withTable(NameAlias.builder("p1").build()).eq(Picture_Table.pictureid.withTable(NameAlias.builder("pi1").build())))
.innerJoin(Flag.class)
.on(SpaceAssociation_Table.flagId.eq(Flag_Table.flagId))
.innerJoin(FlagDefinition.class)
.on(Flag_Table.flagDefinitionId.eq(FlagDefinition_Table.flagDefinitionId));
So I decided it would be a better idea to create a SQL view in my database and query this view :
SQLite.select()
.from(PlantsAssociations.class)
.queryList();
Far more readable ! The problem is that I'm getting this error
database.sqlite.SQLiteException: no such table: PlantsAssociations (code 1): , while compiling: SELECT * FROM PlantsAssociations
If I copy and paste this generated query and execute it directly in SQLite console on my database it works...
I check the official documentation, it says "Declared like tables" :
Views: Declared like tables, Views (Virtual Tables) are supported.
So I declared my view exactly like a table :
#Table(database = PlantsDatabase.class)
public class PlantsAssociations extends BaseModel {
#PrimaryKey
#Column(name = "p0_plant_id")
private int p0PlantId;
#Column(name = "p0_definition")
private String p0Definition;
#Column(name = "p0_picture")
private String p0Picture;
#Column(name = "p1_plant_id")
private int p1PlantId;
#Column(name = "p1_definition")
private String p1Definition;
#Column(name = "p1_picture")
private String p1Picture;
#Column(name = "flag_id")
private int flagId;
#Column(name = "flag_definition")
private String flagDefinition;
public PlantsAssociations() { }
public PlantsAssociations(int p0PlantId, String p0Definition, String p0Picture, int p1PlantId, String p1Definition, String p1Picture, int flagId, String flagDefinition) {
this.p0PlantId = p0PlantId;
this.p0Definition = p0Definition;
this.p0Picture = p0Picture;
this.p1PlantId = p1PlantId;
this.p1Definition = p1Definition;
this.p1Picture = p1Picture;
this.flagId = flagId;
this.flagDefinition = flagDefinition;
}
public int getP0PlantId() {
return p0PlantId;
}
public void setP0PlantId(int p0PlantId) {
this.p0PlantId = p0PlantId;
}
public String getP0Definition() {
return p0Definition;
}
public void setP0Definition(String p0Definition) {
this.p0Definition = p0Definition;
}
public String getP0Picture() {
return p0Picture;
}
public void setP0Picture(String p0Picture) {
this.p0Picture = p0Picture;
}
public int getP1PlantId() {
return p1PlantId;
}
public void setP1PlantId(int p1PlantId) {
this.p1PlantId = p1PlantId;
}
public String getP1Definition() {
return p1Definition;
}
public void setP1Definition(String p1Definition) {
this.p1Definition = p1Definition;
}
public String getP1Picture() {
return p1Picture;
}
public void setP1Picture(String p1Picture) {
this.p1Picture = p1Picture;
}
public int getFlagId() {
return flagId;
}
public void setFlagId(int flagId) {
this.flagId = flagId;
}
public String getFlagDefinition() {
return flagDefinition;
}
public void setFlagDefinition(String flagDefinition) {
this.flagDefinition = flagDefinition;
}
}
But as said before it looks like DBflow generate the right query but there is something wrong by finding the view on SQLite side....
So I checked the official SQLite documentation and it looks like I did it right :
CREATE VIEW PlantsAssociations
as
select p0.plantid as p0_plant_id,
d0.definition as p0_definition,
pi0.picture as p0_picture,
p1.plantid as p1_plant_id,
d1.definition as p1_definition,
pi1.picture as p1_picture,
flag.flagId as flag_id,
flagDefinition.definition as flag_definition
from plant p0
inner join definition d0
on p0.definitionId = d0.definitionId
inner join picture pi0
on p0.pictureId = pi0.pictureId
inner join spaceAssociation
on p0.plantId = spaceAssociation.plantId1
inner join plant p1
on spaceAssociation.plantId2 = p1.plantid
inner join definition d1
on p1.definitionid = d1.definitionid
inner join flag
on spaceAssociation.flagId = flag.flagId
inner join flagDefinition
on flag.flagDefinitionId = flagDefinition.flagDefinitionId
inner join picture pi1
on p1.pictureid = pi1.pictureid
where d0.definition != d1.definition
Did i missed something ?
[EDIT]
I just increased the database version number but now the query just returns me an empty list...
Uninstalled the app then ./gradelw clean and Reintall the app.
it works...
I am getting array list from Global class and getting value with index , but i am getting error on sub-string is that (Sub-string cannot be resolved).
Glide.with(ChooseCategoryProductsActivity.this)
.load("file:///" + GlobalClass.IMAGES_PATH + "/" + GlobalClass.categoriesAr.get(GlobalClass.currentIndex)
.substring(categoriesAr.get(GlobalClass.currentIndex)
.lastIndexOf("/") + 1))
.placeholder(R.drawable.stub)
.into(categoryImage);
Category class :
public class Category {
public String catId = "";
public String catName = "";
public String catImage = "";
public String catDesc = "";
public String displayOrder = "";
public String createdDate = "";
public ArrayList<Product> productsAr = new ArrayList<Product>();
public Category(String catId, String catName, String catImage, String catDesc, String displayOrder, String createdDate) {
this.catId = catId;
this.catName = catName;
this.catImage = catImage;
this.catDesc = catDesc;
this.displayOrder = displayOrder;
this.createdDate = createdDate;
}
}
As you Declared
ArrayList<Category> categoriesAr = new ArrayList<Category>();
subString() is only applied on String not on custom object
There is no such method called subString() with glide as far as i know and hence the compile time error Sub string cannot be resolved. subString works with String
So finally i did just like following, but i don't know whether is it right or not :
GlobalClass.categoriesAr.get(GlobalClass.currentIndex).toString().substring(categoriesAr.get(GlobalClass.currentIndex)
I can't seem to get my projection query to return anything but null, not sure what I'm doing wrong.
Here's the code where I set up and call the query:
Query query1 = OfyService.ofy().load().type(CargoEntity.class).
project("imageUrl", "latitude", "longitude").distinct(false); //filter("group", group);
// Execute the query:
List<Entity> results = query1.list();
logger.warning("(Query from datastore results.isEmpty()) : " + (results.isEmpty()));
logger.warning("(Group = ) : " + group);
if (!results.isEmpty()) {
logger.warning("(Query from datastore results.size()) : " + (results.size()));
//Create STRTree-Index.
STRtree strTree = new STRtree();
GeometryFactory gf = new GeometryFactory();
//Loop through the result list from DataStore.
for (Entity result : results) {
STRLeaf leaf = new STRLeaf((float)result.getProperty("latitude"), (float)result.getProperty("longitude"), (String)result.getProperty("imageUrl"));
Coordinate coord = new Coordinate(leaf.getLongitude(), leaf.getLatitude());
Point point = gf.createPoint(coord);
//Add result to index.
strTree.insert(point.getEnvelopeInternal(), leaf);
}
I'm really new to this so it could be something obvious that i'm missing. I do see the indexes in the developers console though. Here's what the properties in my Entity looks like:
#Entity
#Index
#Cache
public class CargoEntity {
//datastore key
#Id
private String imageUrl;
private float latitude;
private float longitude;
private String group;
#Unindex
private int rating;
#Unindex
private Blob image;
#Unindex
private String email;
#Unindex
private String userName;
#Unindex
private String description;
#Unindex
private Date date;
#Unindex
private String blobKey;
#Unindex
private String type;
#Unindex
private boolean flag;
#Unindex
private int photoOrientation;
public CargoEntity() {
}
//getters and setters below
So apparently I was adding the Entity id to the projection query which is not allowed (or it is allowed but returns null). This post answered the problem.
Google App Engine projection query returns 0 results
This is my entity class. I use three ENUM_INTEGER data type in this class
#DatabaseTable
public class MessageData {
#DatabaseField(generatedId = true, columnName = ID_FIELD_NAME)
private Integer messageId;
#DatabaseField(dataType = DataType.DATE_TIME, columnName = DATE_FIELD_NAME)
private Date date;
#DatabaseField(dataType = DataType.ENUM_INTEGER, columnName = DIRECTION_FIELD_NAME)
private MessageEnums.MessageDirection direction;
#DatabaseField(columnName = CONTACT_ID_FIELD_NAME)
private String contactId;
#DatabaseField(columnName = MESSAGE_FIELD_NAME)
private String message;
#DatabaseField(dataType = DataType.ENUM_INTEGER, columnName = TYPE_FIELD_NAME)
private MessageEnums.MessageType type;
#DatabaseField(columnName = CONTENT_ADDRESS_FIELD_NAME)
private String contentAddress;
#DatabaseField(dataType = DataType.ENUM_INTEGER, columnName = STATUS_FIELD_NAME)
private MessageEnums.MessageStatus status;
#DatabaseField(columnName = READ_FIELD_NAME)
private boolean read;
//.....
}
and this is my enumeration class
public class MessageEnums {
public enum MessageDirection{
IN,
OUT
}
public enum MessageType{
TEXT,
VOICE,
IMAGE,
STICKER,
AUDIO,
VIDEO,
LOCATION
}
public enum MessageStatus{
PENDING,
SENT,
DELIVERED,
RECEIVED
}
}
I use this code to insert a row
newMessageData = new MessageData(date, MessageEnums.MessageDirection.OUT, params[2], params[1]
, MessageEnums.MessageType.TEXT, "", MessageEnums.MessageStatus.SENT, true);
Dao<MessageData, Integer> dao = myApplication.getMessageDatabaseHelper().getMessageDao();
dao.create(newMessageData);
I get this error after running last line
Unable to run insert stmt on object com.x.xx.database.MessageData#41a72790: INSERT INTO messagedata (date ,direction ,contactID ,message ,type ,contentAddress ,status ,read ) VALUES (?,?,?,?,?,?,?,?)
hope somebody help me!
The problem is from date field. when I changed data type from DATE_TIME to DATE_STRING problem solved. I don't know what is the problem of DATE_TIME.