This question already has answers here:
difference between rawquery and execSQL in android sqlite database
(2 answers)
Closed 6 years ago.
I have a databaseHelper class. But, I want to multiply two values of each row and store it in another column while a button is clicked. I have checked my query and it works fine but I want to directly multiply the values and make change in the database when a button is click. I don't want to return any values but simply execute the query.
What I did for this is I have created a method in my database helper class. This is the method:
public void getTicketTotal(){
SQLiteDatabase db=this.getWritableDatabase();
db.rawQuery("update ticket_table set total=quantity*item_price ",null);
}
and in the button click event I did this. But, it didn't multiply and store the values in the table:
myDb=new DatabaseHelper(TicketActivity.this);
myDb.getTicketTotal();
What to do?
The problem was with the method that I was using. The solution is to use db.execSQL instead of rawQuery. So the line of code would be:
public void getTicketTotal(){
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("update ticket_table set total=quantity*item_price ");
}
and simply call it from within the activity.
your update query is wrong. on which row you have to update total?
hope it helps
public void getTicketTotal(){
SQLiteDatabase db=this.getWritableDatabase();
db.rawQuery("update ticket_table tt set total= tt.quantity * tt.item_price" ,null);
}
Related
I am making a quiz app in which i have to select the number of question depending upon user choice.Actually i want to something like this
db.execute('select * from Quiz limit **Variable**')//Here i want to use variable instead of constant value.
Could you help me in this
Thanks in advance
Change your code as:
public void selectall(String variable){
Cursor c=db.rawQuery("SELECT from Quiz limit "+variable);
}
where variable is set according to user selection
NOTE : db.execute is not SQLiteDatabase class method in Android you will need to use execSQL or query methods for quering database table
and see this tutorial for performing all select,delete,update and insert operation on database in android :
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Why not use something like this:
int num = 10;
db.execSQL("select * from Quiz limit " + num)
I am trying to update a single column in a SQLite database table in my Android project. What I want to do is set the value of "done" column to 1, when the vehicle number is same. I preferred to go with sql queries other than using myDB.update. So I used this query,
update 'details' set done = 1 where vehicle=="*****"
But the problem is, this query is working perfectly in the sqlite databse browser, not in android simulator. This is completely not working at the android simulator. Hope ur advice.
Thanks in advance.
//Declaration
SQLiteDatabase dbx;
ContentValues cv1;
EventDataSQLHelper eventsData;//object of class in which table is created
//on create
eventsData = new EventDataSQLHelper(this);
dbx= eventsData.getReadableDatabase();
cv1 = new ContentValues();
cv1.put("done",1);
//update query
dbx.update("details", cv1, "vehicle=" ? , null);
I’m using a database helper to update a table with one row and two fields, I have the following code that that sends two phone numbers through.
dbHelper.updateNumbers(newSmsNumber, newVoiceNumber);
and the following method in the helper.
public void updateNumbers(String newSmsNumber, String newVoiceNumber) {
//Update code here
}
Can anyone show me the code I need to add in the method to update the two fields in the database.
Cheers,
Mike.
ContentValues cv = new ContentValues();
cv.put("SMS", newSmsNumber);
cv.put("Voice", newVoiceNumber);
db.update("[table name]", cv, "ID=?", new String[]{Integer.toString(id)});
There are some gaps to fill up though, the table name, and how you identify the entry you want to update (I put a "ID" field there in that example)
Did not run that code, did not really check, but that should give you an idea.
I am trying to build a query in SQLite in Android. Query is very simple and looks like this:
SELECT
ifnull(object.icon_large, ifnull( object.image, item.IMG_DEF_COVER))
FROM
object , item
WHERE
object.id_item = 1 AND item.id_item = 1;
I've tried to use SQLiteDatabase.query(), but it can be used only for one table. Any suggestions how to combine queries or how to join them, using Android SQLiteDatabases query's syntax?
Thanks in advance!!!
#fox you can use rawquery() instead of query example:
SQLiteDatabase mydatabase;
mydatabase.rawquery("select * from a, b where a.id=b.someid", null);
or can use querybuilder's setTable() method to specify the joins example
builder.setTables(TABLEA+" LEFT JOIN "+TABLEB+" ON "+TABLEB+"."+KEY_ID+"="+TABLEA+"."+KEY_B_ID);
SELECT
ifnull(object.icon_large, ifnull(object.image, item.IMG_DEF_COVER))
FROM
object
JOIN
item
ON
object.id_item = item.id_item
WHERE
object.id_item = 1;
SELECT syntax
i created a database with 6 columns and i have a create and update method in my class that takes 6 parameters/arguments which represent these columns. my problem is that, anytime i try to update or create the database without using all 6 arguments (setting some to null), i get an error "constraint failed". this is most particular with the update method.
any ideas how i can get around this? because sometimes i don't want to fill all columns. I have removed the "text not null" constraint when creating the database. Thank you.
You're going to want to use the ContentValues to achieve this. Heres a quick demo.
My function
public boolean updateStuff(int id,ContentValues args) {
return mDb.update(TableName, args, _id_col + "=" + id, null) > 0;
}
And to call it. Note you can put as many ContentValues as you need
ContentValues initValues = new ContentValues();
initValues.put(col_key,col_value);
Edit:
mDB is a SQLiteDatabase