Android Sqlite update between - android

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

Related

Insert data into SQLite table in the same row

I am trying to insert data in an SQLite database from different places in different columns at a time. When I view the database, each insert takes one row. My databases lookes like this:
How do I achieve this? Any suggestions?
In order to insert a new row within your database, you need to add the following code into the Java class that manages your SQLite database:
public long insertRow(String name, String email)
{
ContentValues contentValues = new ContentValues();
contentValues.put(rowName, name);
contentValues.put(rowEmail, email);
return database.insert(databaseTable, null, contentValues);
}
The variable database refers to the database you attempting to insert the row. The variable databaseTable refers to the table you want to insert the row into. If you are attempting to update an existing column, you need to add the following code into the Java class that manages your database:
public boolean updateRow(long id, String name, String email)
{
ContentValues contentValues = new ContentValues();
contentValues.put(rowName, name);
contentValues.put(rowEmail, email);
return database.update(databaseTable, contentValues, rowId + “=” + id, null) > 0;
}
In order to call this function, you need to pass the id of the row and the desired values to pass in order to update them.

To update the content of the table in Sqlite Database in 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});
}

Update value to specific primary key in sqlite , android?

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.

Android - How to store html string in SQLite Database

I have an html string I would like to store in my SQLite db "as is". The special characters in the html string prevent my INSERT statement from storing it:
INSERT INTO myTable VALUES ('" + htmlString + "')
On iOS I used parameterized queries to accomplish this and it worked fine. How can I accomplish this on Android? I've Google parameterized queries for Android but the results were varied and unclear.
in Android you have parameterized queries too ... are few way to achive this:
ContentValues vals = new ContentValues();
vals.putString("ColumnName", htmlString);
db.insert("myTable", null, vals);
or
final SQLiteStatement insert = db.compileStatement("INSERT INTO myTable VALUES (?)");
insert.bindString(1, htmlString);
//edit: hehe forgot about most important thing
insert.executeInsert();
or
db.rawQuery("INSERT INTO myTable VALUES (?)", new String[] {htmlString});
EDIT: (inserting multiple rows)
if you wana insert more than 1 row then do it in transaction (it should be quicker)
and prefer 2nd solution:
db.beginTransaction();
try {
final SQLiteStatement insert = db.compileStatement("INSERT INTO myTable VALUES (?)");
for(...){
insert.clearBindings();
insert.bindString(1, htmlString[N]);
//edit: hehe forgot about most important thing
insert.executeInsert();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}

Android - populating database column with random values

I am trying to populate a database column with random values like this, it works in sql (each row get a different random value)
UPDATE vyroky SET poradie = RANDOM();
However when I try to do this with a content resolver and provider, it just inserts 0 for some reason
ContentValues values = new ContentValues();
values.put(QuoteContentProvider.COLUMN_ORDER, "RANDOM()");
int count = getContentResolver().update(QuoteContentProvider.CONTENT_URI, values, null, null);
Thanks !

Categories

Resources