Sunday, July 1, 2012

How Do i Work With Android DataBase - Select / Insert / Update / Delete

BeanMatrixDbActivity.java

package beanmatrix.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class BeanMatrixDbActivity extends Activity {
       private SQLiteAdapter SQLiteAdapter1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listContent = (ListView)findViewById(R.id.contentlist);         
        SQLiteAdapter1 = new SQLiteAdapter(this);     
        SQLiteAdapter1.openToWrite();     
        SQLiteAdapter1.deleteAll();     
        SQLiteAdapter1.insert("Apple");     
        SQLiteAdapter1.insert("Banana");     
        SQLiteAdapter1.insert("Watermalon");
        SQLiteAdapter1.insert("Mango");
        SQLiteAdapter1.close();           
        SQLiteAdapter1 = new SQLiteAdapter(this);     
        SQLiteAdapter1.openToRead();     
        Cursor cursor = SQLiteAdapter1.queueAll();     
        startManagingCursor(cursor);     
        String[] from = new String[]{SQLiteAdapter.FRUIT_NAME};     
        int[] to = new int[]{R.id.text};     
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, from, to);     
        listContent.setAdapter(cursorAdapter);         
        SQLiteAdapter1.close();
    }
}
SQLiteAdapter.java

package beanmatrix.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
       public static final String DATABASE_NAME = "DATABASE1";
       public static final String TABLE = "FRUITS";
       public static final int MYDATABASE_VERSION = 1;
       public static final String ID = "_id";
       public static final String FRUIT_NAME = "Content";
       private static final String SCRIPT_CREATE_DATABASE ="create table " + TABLE + " ("+ ID + " integer primary key autoincrement, "+ FRUIT_NAME + " text not null);";
       private SQLiteHelper sqLiteHelper;
       private SQLiteDatabase sqLiteDatabase;
       private Context context;
       public SQLiteAdapter(Context c){context = c;}
       public SQLiteAdapter openToRead() throws android.database.SQLException
       {
              sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, MYDATABASE_VERSION);
              sqLiteDatabase = sqLiteHelper.getReadableDatabase();
              return this;
              }
       public SQLiteAdapter openToWrite() throws android.database.SQLException
       {
              sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, MYDATABASE_VERSION);
              sqLiteDatabase = sqLiteHelper.getWritableDatabase();
              return this;
              }
       public void close()
       {
              sqLiteHelper.close();
              }
       public long insert(String content)
       {
              ContentValues contentValues = new ContentValues();
              contentValues.put(FRUIT_NAME, content);
              return sqLiteDatabase.insert(TABLE, null, contentValues);
              }
       public int deleteAll()
       {
              return sqLiteDatabase.delete(TABLE, null, null);
              }
       public Cursor queueAll()
       {
              String[] columns = new String[]{ID, FRUIT_NAME};
              Cursor cursor = sqLiteDatabase.query(TABLE, columns,  null, null, null, null, null);
              return cursor;
              }
       public class SQLiteHelper extends SQLiteOpenHelper
       {
              public SQLiteHelper(Context context, String name,  CursorFactory factory, int version)
              {
                     super(context, name, factory, version);
                     }
              @Override
              public void onCreate(SQLiteDatabase db)
              {
                     db.execSQL(SCRIPT_CREATE_DATABASE);
              }
     }
 }
             
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <ListView android:id="@+id/contentlist"
                           android:layout_width="fill_parent"
                           android:layout_height="fill_parent"/>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/text"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dip"/>
</LinearLayout>


No comments:

Post a Comment