Studyon Minte9.com
Android




Update



%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.content.ContentValues;
	import android.database.Cursor;
	import android.database.sqlite.SQLiteDatabase;
	import android.os.Bundle;
	import android.widget.Toast;

	public class MainActivity extends Activity {

		@Override
	    protected void onCreate(Bundle state) {
			super.onCreate(state);
			setContentView(R.layout.activity_main);

			SQLiteDatabase db = new MySQLiteHelper(this).getWritableDatabase();
			
			/*--- LOOK HERE ---*/
					
			// Update - method 1
			db.execSQL("UPDATE comments SET comment = 'XXX' WHERE _id = 2");
			
			// Update - method 2
			ContentValues values = new ContentValues();
			values.put("comment", "YYY");
			db.update("comments", values, "_id=2", null);
			
			// Update - method 3
			ContentValues values2 = new ContentValues();
			values2.put("comment", "ZZZ");
			String[] args = {"2"};
			db.update("comments", values2, "_id = ?", args);
			
			// Get comment
			Cursor cursor = db.rawQuery("SELECT * FROM comments WHERE _id = 2", null);
			cursor.moveToFirst();
			Toast.makeText(
                            getApplicationContext(), "id: " + cursor.getLong(0) + " comment: " + cursor.getString(1),
                           Toast.LENGTH_LONG).show();
			
			cursor.close();
		}
	}
	

	%--- MySQLiteHelper.java

	package com.example.myapp;
	import android.content.Context;
	import android.database.sqlite.SQLiteDatabase;
	import android.database.sqlite.SQLiteOpenHelper;

	public class MySQLiteHelper extends SQLiteOpenHelper {
		
		public static final String DB_NAME = "comments.db";
		public static final int DB_VERSION = 1;

		public MySQLiteHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
					
			String sql = "CREATE TABLE comments (" 
					+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "	
					+ "comment TEXT NOT NULL"
					+ ")";
			db.execSQL(sql);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
	}


http://stackoverflow.com/questions/5987863/android-sqlite-update-statement