To update the content of the table in Sqlite Database in Android - android

I have created a database in Sqlite Android Application, there are two fields in the table i.e Name and Email. I want to update the contents of the table, the query that i am using is.
db.update(TABLENAME,cv,"id=?",new String[]{String.valueOf(7)});
As this will update the content that have the ID 7, but i want to update the table by matching the name entered.

Add another Parameter like
db.update(TABLENAME,cv,"id=? AND Name=? ",new String[]{String.valueOf(7),NameValue});

Try
String where="id=? and Name=?";
db.update(TABLENAME,cv,where,new String[]{String.valueOf(7),NameValue});

To match the name instead of the ID, just replace the column name in the whereClause:
db.update(TABLENAME, cv, "Name=?", new String[]{"PanKush"});

pass your string array in update method...
public int Upadte(String name_value,String email_value,String new_name) {
ContentValues value = new ContentValues();
value.put("name",new_name);
return _sqliteDB.update(TABLE_NAME ,value," NAME "+"=?"+" and "+" EMAIL "+"=?",new String[]{name_value,email_value});
}

Related

sqlite - conditional replace and insert

working environment: using sqlite for android os. Doing bulk insert.
consider the following schema or sqlfiddle:
CREATE TABLE employee_data
(
id varchar(20) primary key,
name varchar(20),
dept varchar(20),
updation_date varchar(30)
);
INSERT INTO employee_data
(id, name, dept, updation_date)
VALUES
("1", "john", "tech", "2017-04-30");
INSERT INTO employee_data
(id, name, dept, updation_date)
VALUES
("2", "john2", "tech", "2017-05-01");
While adding an entry to employee_data table, following conditions must be met:
if given entry does not already exists (compared on the basis of id), then add it.
if the id already exists; then check the 'updation_date' column -
if updation_date of entry to be added is later compare to the one that already exists - then update the row.
else do nothing.
Example:
I)
need to insert ("1", "john", "management", "2017-05-01")
as there is already an entry with id = 1, check for updation_date;
now since "2017-05-01" is more recent data - update the row.
so ouput - ("1", "john", "management", "2017-05-01")
II)
need to insert ("3", "steve", "management", "2017-06-01")
as no entry with id= 3 in employee_data, directly add this one
output: ("3", "steve", "management", "2017-06-01") is added.
III)
("2", "john2", "tech", "2017-04-01");
as id=2 row exists with more recent updation_date ("2017-05-01")
do nothing.
I have tried to search but could not find solution. I can't figure out how can I use insert into select statement. Or whether I can use select from dual.
I remember using merge in oracle for similar use case.
Note:
updation_date column can be changed to timestamp format or date format for ease in comparing it.
Thanks.
SQLite is an embedded database; you are supposed to implement complex logic in your application:
void conditionalReplaceAndInsert(ContentValues cv)
{
String oldDate = DatabaseUtils.stringForQuery(db,
"SELECT updation_date FROM employee_data WHERE id = ?",
new String[]{ cv.getAsString("id") });
if (oldDate.equals(""))
db.insertOrThrow("employee_data", null, cv);
else if (oldDate.compareTo(cv.getAsString("updation_date")) < 0)
db.update("employee_data", cv, "id = ?",
new String[]{ cv.getAsString("id") });
else
; // do nothing
}

Android Sqlite update between

I want to update my table data base on _id between range like 120 to 150. How can I update data using update or how can I do it ?
you can use the following code :-
ContentValues cv = new ContentValues();
cv.put("Field1","Bob"); //These Fields should be your String values of actual column names
cv.put("Field2","19");
cv.put("Field2","Male");
myDB.update(TableName, cv, "_id>=120 and _id<=150", null);

Android and SQLlite: escape quote and get it back

I have to save some data inside of SQLLite. This text data can contain a quote ('). Is there a way to escape this char on insert, and get it back when getting the data from the database?
In particular, the name is a references to a file. So the file can be named like "hel'lo.file". Before escaping it to the database, it should be "hel''lo.file". But when i get it back i need again "hel'lo.file" to be certain that the string inside the db matches the file name.
I'm using a content provider and a SQLiteOpenHelper.
Inside my content provider, for the insert i'm doing this:
_id = db.insert(TextEditorContract.NoteEntry.TABLE_NAME, null, values);
My insert inside my activity:
ContentValues values = new ContentValues();
...
values.put(NoteEntry.COLUMN_TITLE, getFileTitle());
Uri recordUri = contentResolver.insert(NoteEntry.CONTENT_URI, values);
Use SQLiteOpenHelper - then you can use prepared statements. See this question: Android SQLite Example
EDIT
String file = "/storage/emulated/0/Note/hello 'world.txt"
String sql = "SELECT _id FROM Recents WHERE percorso=? ORDER BY _id ASC";
String[] data = {file};
Cursor cursor = database.rawQuery(sql, data);

Delete entire row from SQLite android using two given column data

I want to delete a specific row in SQLITE data in android.Now, I already try only using only one column data like primary key but now I want to delete an entire row using a column key and column name .How will I do this?help me
Below is the syntax I made to delete, but their nothing happen?
DatabaseHandler.java
public Cursor delete(String id, String name){
SQLiteDatabase data=this.getWritableDatabase();
String selectQuery = "DELETE FROM Criteria WHERE criteria_eventpk ="+"'"+id+"' AND criteria_eventpk ="+"'"+name+"'" ;
Cursor idupdate = data.rawQuery(selectQuery, null);
data.close();
return idupdate;
I use rawQuery because I try db.delete but only one data is only allowed.So I try rawQuery .
Next, the codes in the button delete in my MainActivity
evcri_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
db.delete(evpk.getText().toString(), cripk.getText().toString());
}
});
I think I made a wrong choice to choose cursor, Can anyone help me?I guess a custom query is needed but I need your help.Help me..
I try this ,but still it doesn't work.In their any wrong code I use?
public void delete(Criteria cri, Criteria pk){
SQLiteDatabase data=this.getWritableDatabase();
data.delete(TABLE_CRITERIA,"criteria_name=? AND criteria_eventpk=?", new String[]{String.valueOf(cri.get_criname()),String.valueOf(pk.get_eventpk())});
data.close();
In my delete button
db.delete(evpk.getText().toString(), cripk.getText().toString());
But when I try this code below, it works but only the id is given,I want the given are id and name..
data.delete(TABLE_CRITERIA,"criteria_name=?, new String[]{String.valueOf(cri.get_criname())});
I use rawQuery because I try db.delete but only one data is only allowed.So I try rawQuery .
This information is not correct. The db.delete method allows multiple parameters:
public int delete(String id, String name){
SQLiteDatabase data = this.getWritableDatabase();
String table = "Criteria";
String whereClause = "criteria_eventpk=? AND criteria_eventpk =?" ;
String whereArgs[] = new String[] {id, name};
int count = data.delete(table, whereClause, whereArgs);
data.close();
return count;
}
Try this - I put the single quotes inside the double quotes and it always works for me
String selectQuery = "DELETE FROM Criteria WHERE criteria_eventpk ='"+id+"' AND criteria_eventpk ='"+name+"'" ;
Your statement is of the form:
DELETE FROM Criteria WHERE criteria_eventpk = /id/ AND criteria_eventpk = /name/
So criteria_eventpk has to equal both the value of id and the value of name. That will only be the case where id and name are the same which I imagine is not usually going to be the case?
Did you want to delete where criteria_eventpk is equal to either id or name? If so change the AND to OR. Or do you mean to check two different columns in the row? In which case one of the criteria_eventpk's needs changing.

How to alter database value?

I have a dialog that has 3 buttons (OK, Alterar, Delete) so when i click in Alterar it goes to another activity to alter the values, but i dont know the code to alter it.
I only know 2, which are .put/.insert and .delete .
Now what about alter?
2 rows: nome , telefone
Allright, this is an answer to your edit.
Let's say your table containing your rows has the name Person.
Now you want to change the phone number of the person "MrSmith" to 123456789.
First, enter the new values you want to change.
// Values to insert
ContentValues dataToInsert = new ContentValues();
dataToInsert.put("name", "MrSmith");
dataToInsert.put("phone", 123456789);
Maybe you dont need to put the name back in again, it was some time ago i did this. You can try out yourself :)
Now lets insert these into the database!
// We want to update the row where the name is "MrSmith"
String where = "id=?";
String[] whereArgs = { "MrSmith" };
The ? in where string is replaced with MrSmith when we do db.update(...).
Now SQL knows what row to update and what data it should update it with. Let's commit!
// Update table Person where the row name is "MrSmith" with the values entered
// in ContentValues!
db.update("Person", dataToInsert, where, whereArgs);
I hope this helps! Otherwise you need to check out how SQL Statements are done.
EDIT Function
public void editdata(int id,String name)
{
db.execSQL("update person set name='"+name+"' where id="+id);
}
Call function
editdata(id,"test");

Categories

Resources