0

I'm currently developing an Android app. The database has 1 column only named content. Here it is the code:

 public long insert(String content){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_CONTENT, content);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

How to make an update for a specific column (according to its index) in that database?

Bobby Chandra
  • 89
  • 2
  • 10
  • If that's really the only column and you don't have an id column, you have a problem. – Joachim Isaksson May 06 '13 at 16:19
  • I used to put an _id column but it always forcequit everytime the app runs. After trying few solutions, the only solution that works is making a database with one column only – Bobby Chandra May 06 '13 at 16:23
  • Without an id column, you have nothing to identify the row except the content of your column. There is no such thing as order in a database table unless you have something to order by, so you can't "update the third row" for example. – Joachim Isaksson May 06 '13 at 16:26

2 Answers2

1
db.update
(
  MYDATABASE_TABLE, contentValues , ID_FIELD_CONST + " = ?", 
  new String[] { idValue }
);
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
bclymer
  • 6,679
  • 2
  • 27
  • 36
  • Thank you for answering, I have a question. The idValue is the data which going to replace the old data in that column, is that right? – Bobby Chandra May 06 '13 at 16:19
  • No, that's the data you use to find the row you want to update. The only values that will actually be updated are the ones in contentValues. So if you add 2 columns of data those 2 will be updated, if you add 1, then 1 will be updated and the other left intact. – bclymer May 06 '13 at 16:22
  • I see. So, let say I want to update the column with index number 3. The index number will be retrieved by "ID" The update method is going to be like this: public boolean updateContent(long ID) { ContentValues args = new ContentValues(); args.put(KEY_CONTENT,content); return db.update ( MYDATABASE_TABLE, args , ID_FIELD_CONST + "=", new String[] { ID } ); } is that right? – Bobby Chandra May 06 '13 at 16:34
0

You can use:

ContentValues cv = new ContentValues();
cv.put(KEY1,value1);
cv.put(KEY2,value2);
//... all the fields you want to update
String where = KEY_CONTENT + " = " + value;
db.update(table_name,cv,where,null);

KEY1,KEY2 ... are the name of the fields you want to update value1,value2... are the new values of the fields you want to update the where String is to tell in which row you need to update.

Ron537
  • 990
  • 1
  • 9
  • 20