Studyon Minte9.com
Android




Delete



        %--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	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 ---*/
					
			// Delete
			db.delete("comments", "_id=1", null);
                        //db.execSQL("DELETE FROM articles WHERE _id=" + article.getId());

			
			// Get comment
			try {
				Cursor cursor = db.rawQuery("SELECT * FROM comments WHERE _id = 1", null);
				cursor.moveToFirst();
				Toast.makeText(
					getApplicationContext(), cursor.getString(1), Toast.LENGTH_LONG).show();
				cursor.close();
			} catch (Exception e) {
				Toast.makeText(
					getApplicationContext(), "Comment not exists", Toast.LENGTH_LONG).show();
			}
		}
	}

	%--- 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) {
			
			// db.execSQL("DROP TABLE IF EXISTS comments");
			// onCreate(db);
		}
	}


http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial_ui