Studyon Minte9.com
Android




Select



       %--- 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 ---*/
			
			// Get comment
			Cursor cursor;
			
			// Method 1 - table/fields
			String[] fields = {"_id", "comment"};
			cursor = db.query("comments", fields , "_id = " + 2, null, null, null, null);
			
			// Method 2 - args
			String[] args = {"2"};
			cursor = db.rawQuery("SELECT * FROM comments WHERE _id = ?", args);
			
			// Method 3 - straight
			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();
                       
                       // don't forget to close the cursor
		       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://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial_ui
http://stackoverflow.com/questions/7387455/android-sqlite-how-to-retrieve-specific-data-from-particular-column