Working SQLite – Android

Hi All,
Tomorow, I have a report very funy and topic is Connect database with mobile platform. I feel that, database not strong point with me but in this small report I only talk about these proplem very basic to you can be working with sqlite the best easy. I has been write a small project to demo and help you (can be biginer with android) more understand about code and the way that android work with Sqlite.ok
Download source code at here

1: DBHelper.java file with content below:

/*
 /////////////////////////////////////////////////////////////////////////
 * Author: Nguyen Van Kien                                               *
 * Code : 092937                                                         *
 * Project Topic : Tutorial working with database in android platform        *
 * Date: 27/05/2011                                                      *
 /////////////////////////////////////////////////////////////////////////
 * */
package hoasen.mobile.edu.vn;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	/*khai bao ten database*/
	final static String DATABASE_NAME = "DBMS1";
	
	/*khai bao version cua database*/
	final static int DATABASE_VERSION = 1; 
	/*khai bao ten bang*/
	public final static String TABLE_NAME_1 = "OPENSOURCE";
	
	/*khai bao cac truong trong bang */
	final static String colID = "IDPRODUCT";
	final static String colVERSION = "VERSION";
	
	
	/*phuong thuc khoi tao cho DBHelper
	 * cac tham so truyen vao:
	 * 1 -> mot doi tuong Context de biet database nay duoc tao trong ung dung nao
	 * 2 -> name la tham do cho biet ten database
	 * 3 -> chua can quan tam toi
	 * 4 -> version cua database
	 * */
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		Log.v("CONFIG :", "CONSTRUCTOR DATABASE");
		// TODO Auto-generated constructor stub		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String quey = "CREATE TABLE " + TABLE_NAME_1 + "("+
					colID + " INTEGER PRIMARY KEY, "+
					colVERSION + " TEXT)";
		
		/*thuc thi cau lenh SQL o tren*/
		db.execSQL(quey);
		Log.v("CONFIG :", "onCreate Method");	
	}

	/*Phuong thucn*/
	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub
		String queryupdate = "DROP TABLE IF EXISTS "+ TABLE_NAME_1;
		db.execSQL(queryupdate);
		onCreate(db);
		Log.v("CONFIG :", "onUpgrade Method");	
	}

}

2: finaly I have program.java file to working with database created :

/*
 /////////////////////////////////////////////////////////////////////////
 * Author: Nguyen Van Kien                                               *
 * Code : 092937                                                         *
 * Project Topic : Tutorial working with database in android platform        *
 * Date: 27/05/2011                                                      *
 /////////////////////////////////////////////////////////////////////////
 * */

package hoasen.mobile.edu.vn;

import hoasen.edu.vn.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class program extends Activity {

	Context context;
	DBHelper db;
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tv = (TextView) findViewById(R.id.text);
		
		db = new DBHelper(this);
		//addVersion("Android");
		updateVersion("Android", "Symbian");
		//deleteVersion("java");
		//deleteVersion("android");
		//deleteVersion("iphone");
		//deleteVersion("samsung");
		
		//deleteVersion("Android OS");
		//deleteVersion("Java progamming");
		//deleteAll();
	    Cursor cursor = getCursor();
	    displayAllVersion(cursor);
	}
	
	private void addVersion(String title) {
		
	    SQLiteDatabase mydb = db.getWritableDatabase();
	    ContentValues values = new ContentValues();
	    
	    //values.put(db.colID, id);
	    values.put(db.colVERSION, title);   
	    mydb.insert(db.TABLE_NAME_1, null, values);

	  }

	  private Cursor getCursor() {
		  
		/*
		 * Khoi tao mot doi tuong SQLiteDatabase 
		 * va cho no quyen co the doc du lieu tu database 
		*/
	    SQLiteDatabase mydb = db.getReadableDatabase();
	    /* Tra ve tap cac ket qua trong bang chi dinh */
	    Cursor cursor = mydb.query(DBHelper.TABLE_NAME_1, null, null, null, null,null, null);	    
	    startManagingCursor(cursor);
	    return cursor;
	  }

	  private void updateVersion(String oldversion, String newversion){
		  /*
		   Cho phep ghi du lieu xuong database
		   * */
		  SQLiteDatabase mydb = db.getWritableDatabase();
		  /*
		   Khoi tao doi tuong ContentValues de thuc hien insert du lieu
		   * */
		  ContentValues update = new ContentValues();
		  update.put(DBHelper.colVERSION, newversion);
		  
		  mydb.update(DBHelper.TABLE_NAME_1, update, DBHelper.colVERSION + "='" + oldversion +"'", null);
	  }
	  
	  /* Delete theo menh de where */
	  private void deleteVersion(String version){
		  
			/*
			 * Khoi tao mot doi tuong SQLiteDatabase 
			 * va cho no quyen co the ghi du lieu vao database 
			*/
		  
		  SQLiteDatabase mydb = db.getWritableDatabase();
		  mydb.delete(DBHelper.TABLE_NAME_1, DBHelper.colVERSION + "='" + version + "'",null);
		  mydb.close();
	  }
	  
	  /* Delete tat ca cac du lieu co trong bang chi dinh */
	  private void deleteAll(){
		  SQLiteDatabase mydb = db.getWritableDatabase();
		  mydb.delete(DBHelper.TABLE_NAME_1, null, null);
		  mydb.close();
	  }
	  
	  private void displayAllVersion(Cursor cursor) {
		  
	    StringBuilder title = new StringBuilder();
	    /*
	     Neu con ban ghi thi con lay du lieu
	     * */
	    while (cursor.moveToNext()) {
	    	
	    	int id = cursor.getInt(0);
	    	// tra ve gia tri cua cot co chi so la 1
	    	// chi so phu thuoc khai bao thu tu cot trong database 
	    	String version = cursor.getString(1);
	    	title.append(id + ": " + version + "\n");
	    }
	    tv.setText(title);
	  }
	
}
Advertisements

4 thoughts on “Working SQLite – Android

    1. Sharing Post author

      ok, tối tớ update source lên, đang online trên máy bàn. source để trên laptop.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s