Sqlite updating a record - android

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

Related

Sync several DATA from Android SQLLite to MariaDB

i look for an easy way to sync my in SQLLite stored Data to MariaDB SQL Database.
WebService is no problem, but i have no clue to get several Data sync at once.
I had an DBAdapter with this, the id is store in sharedpreferences:
public projekt getprojekt(int id){
// 2. build projekt query
Cursor cursor =
db.query(DATABASE_TABLE_P, // a. table
TABLE_P_COLUMNS_ALL, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. build projekt object
projekt projekt = new projekt();
projekt.setId(Integer.parseInt(cursor.getString(0)));
projekt.setKostenstelle(cursor.getString(1));
projekt.setAG(cursor.getString(2));
....
// 5. return projekt
return projekt;
}
and this:
// Get Room List
public ArrayList<HashMap<String, String>> GetRaum(int pid){
ArrayList<HashMap<String, String>> raumList = new ArrayList<>();
String query = "SELECT * FROM "+ DATABASE_TABLE_R + " where pid = '"+ pid +"' order by "+KEY_R_BEZEICHNUNG+" asc";
Cursor cursor = db.rawQuery(query,null);
while (cursor.moveToNext()){
HashMap<String,String> raum = new HashMap<>();
raum.put("id",cursor.getString(cursor.getColumnIndex(KEY_M_ID)));
raum.put("bezeichnung",cursor.getString(cursor.getColumnIndex(KEY_M_BEZEICHNUNG)));
raumList.add(raum);
}
return raumList;
}
Bit how i get several "Rooms" selected to send it via POST/JSON to WebService?
My current function to send Data is not finished ... just getting the projekt_id from sharedpreferences.
public void postData() throws JSONException {
final SharedPreferences sharedPreferences = this.getSharedPreferences("de.dasal.dguvv3", Context.MODE_PRIVATE);
final Integer projektidsp = sharedPreferences.getInt("projekt_id", 0);
final projekt projekt = db.getprojekt(projektidsp);
}
If you need further Informations, please let me know.
Its my first Question on stackoverflow.

how to update a SQQLite database with particular row?

while updating in database, I'm not getting the id of particular row
here is my code for update Note in database
public int updateNote(int id, String tittle, String content , String update_at)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_TITTLE,tittle);
values.put(Note.COLUMN_CONTENT,content);
values.put(Note.COLUMN_TITTLE,update_at);
return db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);
}
Please help me.
update returns the number of rows affected and not the id of the row.
In your case if successful it would return 1.
About the id you need you already have it.
As mTak outlined, the update method returns the number of rows affected. You can also see that in the documentation.
If you want your method to return the ID of the row you updated, then return the id that you pass in, since that's how you select the row to update in the first place:
public int updateNote(int id, String tittle, String content , String update_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_TITTLE,tittle);
values.put(Note.COLUMN_CONTENT,content);
values.put(Note.COLUMN_TITTLE,update_at);
db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);
return id;
}
In your Activity Class:
//*Note is your setterClass
//*notesList is your ArrayList or list
//*position refers your current list position
//*setNote is a method in Note Class
private void updateNote(String note, int position) {
Note n = notesList.get(position);
// updating note text
n.setNote(note);
// updating note in db
db.updateNote(n);
// refreshing the list
notesList.set(position, n);
mAdapter.notifyItemChanged(position);
}
In your database class try below code:
public int updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_NOTE, note.getNote());
// updating row
return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(note.getId())});
}
here i am trying to update single string.i hope this is workig to you.Let me know is this working or not.

Updating rows in Android SQlite

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

How to keep the database updated when the user enters new values in android studio?

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

updating the values in android database

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

Categories

Resources