I have a settings tab in my application which takes an int value through edittext from user and stores it into the database. When the user now wants to change the setting the old setting should be updated in the database. How do I write the database code for that.
Below is my code to save the value in db:
DatabaseHelper dha = new DatabaseHelper(this);
SQLiteDatabase db = dha.getWritableDatabase();
Log.d("","Done with database Sqlite");
ContentValues cv = new ContentValues();
EditText editText2 = (EditText) this.findViewById(R.id.editText1);
setTime = editText2.getText().toString();
Log.d(setTime, "This is the setTime value");
cv.put(DatabaseHelper.TIME, setTime);
db.insert("finalP", DatabaseHelper.TIME, cv);
db.close();
You should do something like this:
// Create content values that contains the name of the column you want to update and the value you want to assign to it
ContentValues cv = new ContentValues();
cv.put("my_column", "5");
String where = "my_column=?"; // The where clause to identify which columns to update.
String[] value = { "2" }; // The value for the where clause.
// Update the database (all columns in TABLE_NAME where my_column has a value of 2 will be changed to 5)
db.update(TABLE_NAME, cv, where, value);
Related
This is my first time meddling with sqlite on android.
Followed a tutorial and created a SQliteHelper Class, there is this method inside:
// Updating single profile
public int updateProfile(Profile profile) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("cpf", profile.getCpf()); // get cpf
values.put("nome", profile.getNome()); // get nome
values.put("phone", profile.getPhone()); // get nome
// 3. updating row
int i = db.update(TABLE_PROFILES, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(profile.getId()) }); //selection args
// 4. close
db.close();
return i;
}
However, I have no idea how to use it..
I know that in order to get the profile I do the following:
MySQLiteHelper db = new MySQLiteHelper(view.getContext());
db.updateProfile(db.getProfile(0));
But how do I actually update it?
That code updates the profile with an specific id in this part:
// 3. updating row
int i = db.update(TABLE_PROFILES, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(profile.getId()) }); //selection args
So, this id comes from the Profile object you have just pased to the function.
You should do something like:
// get a profile
MySQLiteHelper db = new MySQLiteHelper(view.getContext());
Profile p = db.getProfile(0);
// change something
p.setPhone(55598789);
// update profile p
updateProfile(p);
I'm using SQlite in my Android app and the task is - how can I update all the rows in one table?
I have a 1st column with name "cl_id" (integer numbers 1-2-3-4..) and after deleting some rows, I wan't to make a cycle to fill this 1st column with a new values to keep them in right order.
I was trying to execute:
data.put("cl_id",index);
db.update("mdb_table_contactList", data, null, null);
but it's updates all the values in the first column :(
To update the field in specific row, try this;
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id="+_id, null);
db.close();
It's good practice to have a primary key for each row data.
Here _id would be your primary key.
"just give rowId and type of data that is going to be update in ContentValues."
public void updateStatus(String id , int status){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("status", status);
db.update(TableName, data, "columnName" + " = "+id , null);
}
Try this it must work
void updateDatail(String id) {
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id=?", new String[]{id});
db.close();
}
I am trying to get an SQLite database to work in Android and have a few questions!
I have put the following in the onCreate method of my database class, which extends SQLiteOpenHelper.
db.execSQL("CREATE TABLE IF NOT EXISTS `challenge` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `date` TEXT )");
I then have the following method.
public void addChallenge(){
SQLiteDatabase db = this.getWritableDatabase();
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
ContentValues values = new ContentValues();
values.put("date", fDate);
db.insert("challenge", null, values);
}
And the following would grab the challenge added above back.
public int getChallengeId(){
SQLiteDatabase db = this.getWritableDatabase();
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
String countQuery = "SELECT id FROM challenge WHERE date = " + fDate;
Cursor cursor = db.rawQuery(countQuery, null);
int challenge_id = 0;
if(cursor.moveToFirst()){
challenge_id = cursor.getInt(0);
}
return challenge_id;
}
My issue is that the challenge ID is returning as 0.
So I have a few questions.
Where is the database stored by default within Android?
Am I calling this correctly?
The code I use to access the above methods is as follows
Database db_add = new Database(context);
db_add.addChallenge();
db_add.close();
Database db_get = new Database(context);
challenge_id = db_get.getChallengeId();
db_get.close();
Any ideas?
Thanks.
The SQL query is not correct.
2015-02-23 tells the database that you want to subtract the numbers 2 and 23 from the number 2015.
Strings must be 'quoted'.
However, you should avoid such formatting problem by using parameters:
String countQuery = "SELECT id FROM challenge WHERE date = ?";
Cursor cursor = db.rawQuery(countQuery, new String[]{ fDate });
I have to below table structure of my sqlite database in android
I want to update value to "answer" column according to the ques id.
How do I do it?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_Answer, answer);
// Inserting Row
db.insert(TABLE_Question, null, values);
db.close(); // Closing database connection
I want to insert value to "answer" column according to the ques id.
This operation called "update" instead of "insert".
insert: means to add new record in table,
update: means change value of any exist row columns
so required operation is update.use update method as:
String where = "Ques_id=?";
String[] whereArgs = new String[] {String.valueOf(Ques_id)};
ContentValues values = new ContentValues();
values.put(KEY_Answer, answer);
db.update(TABLE_Question, values, where, whereArgs);
insert() inserts a new row. To update a column on an existing row, use update() with a selection like "Ques_id=" + id.
I'm dealing with a simple app, which basically stores various dates. But when I enter a new date, I have to close the app and open it again. The same thing counts for changing the date values. I can only see the changes by closing the app and reopening it.
Here are the two main functions in my DB class;
public void addBirthday(Person person){ //Add a data to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public int updateBirthday(Person person) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
// 3. updating row
int i = db.update(TABLE_NAME, //table
cv, // column/value
column_id +" = ?", // selections
new String[] { String.valueOf(person.getId()) }); //selection args
// 4. close
db.close();
return i;
}
You will need to refresh the view (probably a ListView) where you display this data.
Get rid of the notifyDataSetChanged() and call requery() on your Cursor