I can't find anything about this on the web.
I need to edit all values in a column in my database but I can't find a way.
I'm doing:
if (upgradeCursor.moveToFirst()){
do {
db.editListsColumn("0");
} while (upgradeCursor.moveToNext());
}
Where upgradeCursor:
return mDb.query(ProductsData.PRODUCTS_TABLE, null, null, null, null, null, null);
And editListsColumn("0"):
public void editListsColumn(String lists) {
mDb=mDbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ProductsMetaData.PRODUCT_PRODUCT, lists);
mDb.update(ProductsData.PRODUCTS_TABLE, cv, "lists=?", null);
}
I'm stuck here.
Any suggestion would be really appreciated at the moment! Thank you so much.
Have you tried a simple update without the where clause and without iterating the cursor. What I mean is simply call this:
mDb=mDbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ProductsMetaData.PRODUCT_PRODUCT, lists);
mDb.update(ProductsData.PRODUCTS_TABLE, cv, null, null);
Without:
if (upgradeCursor.moveToFirst()){
do {
db.editListsColumn("0");
} while (upgradeCursor.moveToNext());
}
From the documentation of the update(String table, ContentValues values, String whereClause, String[] whereArgs) method: passing NULL in whereClause will update all rows.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Related
I want to update a field in my database, but the changes not saved to database.
I use The "update" method in database class but not work
Please
This is my Method
public void myupdatedb(int id,String value){
ContentValues cv = new ContentValues();
cv.put("fav", "1");
mydb.update("contents", cv, "id" + "=" + id , null);
}
and in the MainActivity:
db.myupdatedb(1,"1");
From Android documentation
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Try
mydb.update("contents", cv, "id = ?", new String[]{Integer.toString(id)});
In the table I have 4 fields (note1, note2, note3, Note4). I want to delete all the data in note1 and note2. I have to add arguments to the method db.delete, but I have trouble.
public void delete() {
SQLiteDatabase db= mHelper.getWritableDatabase();
db.delete(noteTable.TABLE_NAME, null, null);
}
Delete always deletes entire rows.
To clear values in specific columns, use an update query, for example
ContentValues cv = new ContentValues();
cv.putNull("note1");
cv.putNull("note2");
db.update(noteTable.TABLE_NAME, cv, null, null);
Hello everybody I would like to insert multiple records to my sqlite table at once,
This is what I've tried:
DBHelper.class
public void insertToOUTTRANS(ArrayList<String> arrListDocumentNumber){
ContentValues cv = new ContentValues();
for(String text : arrListDocumentNumber){
cv.put(KEY_DOCUMENTNUMBER, text);
}
myDataBase.insert(TBL_OUTTRANS, cv, null);
}
But I'm getting error in this line:
myDataBase.insert(TBL_OUTTRANS, cv, null);
Error says:
The method insert(String, String, ContentValues) in the type SQLiteDatabase is not applicable for the arguments (String, ContentValues, null)
MainActivity.class:
for(int i = 0; i< arrDocumentNumber.length; i++) {
ArrayList<String> arrListDocumentNumber = new ArrayList<String>();
ArrayList<String> arrListUnitOfMeasure = new ArrayList<String>();
ArrayList<String> arrListLocationCode = new ArrayList<String>();
arrListDocumentNumber.put(tvItemCode.getText().toString());
arrListUnitOfMeasure.put(editText1.getText().toString));
arrListLocationCode.put(editText1.getText().toString));
}
But I'm kinda confused how to loop over the cursor to check for all items from the listview to save to my table. Any ideas? I would gladly appreciate your help. Thanks.
Try this
for(int i=0;i<arrListDocumentNumber.size();i++)
{
ContentValues initialValues=new ContentValues();
initialValues.put(KEY_DOCUMENTNUMBER, arrListDocumentNumber.get(i));
db.insert(TBL_OUTTRANS, null, initialValues);
}
The insert methods looks like..
insert(String, String, ContentValues)
So you need to pass arguments like..
myDataBase.insert(TBL_OUTTRANS, null, cv);
Check syntax here.
myDataBase.insert(TBL_OUTTRANS, null, cv);
instead of
myDataBase.insert(TBL_OUTTRANS, cv, null);
In my Android App, I am trying to insert names in a column forcefully into database but In database only last name is showing i.e. Myself because names are overlapping. Actually My database portion is weak.
This is what I am doing in my Database Handler Class,
public void addPoliticianNames()
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
values.put(KEY_POLITICIANNAMES, "Me");
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
}
What to do for getting these names sequentially into the column (Here I have to insert names forcefully as I have mentioned above). So, Is there any method for this ? Is there any role of cursor in doing this job (like movetonext method or something like that) ?
Please help me,
Thanks.
you have to make one loop for inserting data into database. it will pick one by one value from array and put it into database.
Put this method in your database class
public void deleteTable()
{
database.delete(TABLE_POLLINGVOTES, null, null);
}
db.deleteTable(); // call it with this line for delete all records of your table
// for inserting data into table.
String[] a = {"I","Me","MySelf"};
ContentValues values = new ContentValues();
for (int i = 0; i < a.length; i++)
{
values.put(KEY_POLITICIANNAMES, a[i]);
db.insert(TABLE_POLLINGVOTES, null, values);
}
Hope it will help you.
If you want to insert three records, you have to use three insert calls.
For example:
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Me");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
#Shiv change the column name for every value
public void addPoliticianNames(String ss)
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, ss);
}
ss contain value firts time "I",second time "me" like that
I have an app with a pre-made database. I want to change an entry in this database and save it, but I'm running into some trouble. I have a DatabaseHelper with the following method inside:
public void updateStats(int rowId, int correct, int attempts) {
//Method for updating the stats after a question is answered
SQLiteDatabase db = this.getWritableDatabase();
ContentValues args = new ContentValues();
args.put("CORRECT", correct);
args.put("ATTEMPTS", attempts);
db.update("CHARACTERS", args, "_id=" + rowId, null);
Cursor c = db.query("CHARACTERS", null, null, null, null, null, null);
c.moveToPosition(rowId-1);
Log.d("database", "test - " + String.valueOf(c.getInt(0)));
db.close();
}
It's seems to be running the update fine, because the query is showing that the change has been made in the log. But it's the app is reverting back to the original database when I end the activity.
My knowledge of using databases in Android is patchy, so I'm probably missing something simple. Does anyone know what's going wrong?
please try to use this
ContentValues dataToInsert = new ContentValues();
dataToInsert.put(ALBUM_IS_COMPLETE, "true");
String where = "album_id=?";
String[] whereArgs = {albumid};
try
{
openDataBase();
myDataBase.update(DATABASE_ALBUM_TABLE, dataToInsert, where, whereArgs);
}
catch (Exception e)
{
}
hope this will help you