SQLiteDatabase wrapper : SQLiteDatabase « Database « Android





SQLiteDatabase wrapper

    
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

class DataBase 
{
  public static final String DEBUG_TAG = "DataBase";
  
  public static final String QUREY_CREATE_TABLE = "CREATE TABLE \"_2013_\" (\"id\" INTEGER PRIMARY KEY NOT NULL DEFAULT 1, \"month\" INTEGER NOT NULL DEFAULT 0, \"day\" INTEGER NOT NULL DEFAULT 0, \"incoming_call_number\" INTEGER NOT NULL DEFAULT 0, \"incoming_call_time\" INTEGER NOT NULL DEFAULT 0, \"incoming_message_number\" INTEGER NOT NULL DEFAULT 0, \"outgoing_call_number\" INTEGER NOT NULL DEFAULT 0, \"outgoing_call_time\" INTEGER NOT NULL DEFAULT 0, \"outgoing_message_number\" INTEGER NOT NULL DEFAULT 0, \"total_call_number\" INTEGER NOT NULL DEFAULT 0, \"total_call_time\" INTEGER NOT NULL DEFAULT 0, \"total_message_number\" INTEGER NOT NULL DEFAULT 0)";
  
  SQLiteDatabase mDataBase = null;
  
  Context mContext = null;
  
  public DataBase(Context context)
  {
    mContext = context;
  }
  
  public boolean isExist( String databaseName )
  {
    boolean result = false;

    String[] list = mContext.databaseList();
    
    for( String name : list )
    {
      if( name.equals(databaseName) )
      {
        result = true;
      }
    }
    
    return result;
  }
  
  public boolean create(String name)
  {
    boolean result = false;
    
    try
    {

      mDataBase = mContext.openOrCreateDatabase(name, SQLiteDatabase.CREATE_IF_NECESSARY, null);
      
      result = true;
    }
    catch(SQLiteException e)
    {
      Log.e(DEBUG_TAG, e.getMessage());
    }
    
    return result;
  }
  
  public boolean open(String name)
  {
    boolean result = false;
    
    if(mDataBase == null)
    {
      try
      {
        mDataBase = mContext.openOrCreateDatabase(name, SQLiteDatabase.OPEN_READWRITE, null);
        
        result = true;
      }
      catch(SQLiteException e)
      {
        Log.e(DEBUG_TAG, e.getMessage());
      }
      
    }
    
    return result;
  }
  
  
  public void insert(String tableName, ContentValues values)
  {
    try
    {
      mDataBase.insertOrThrow(tableName, null, values);
    }
    catch(SQLException e)
    {
      Log.e(DEBUG_TAG, e.getMessage());
    }
  }
  
  public void deleteAllRecord(String tableName)
  {
    mDataBase.delete(tableName,null, null);
  }
  
  public void createTable(String query)
  {
    mDataBase.execSQL(query);
  }
  
  public final ArrayList<HashMap<String, Integer>> getData(String tableName)
  {
    Cursor c = null;
    
    c = mDataBase.query(tableName, null, null, null, null, null, null);
    
    // ????? ??? ????? ???? ?? ??
    int columnSize = c.getColumnCount();
    // ????? ?????????? ????
    int resultSize = c.getCount();
    
    ArrayList<HashMap<String, Integer>> data = new ArrayList<HashMap<String,Integer>>(resultSize);
    
    c.moveToFirst();
    
    while(c.isAfterLast() == false)
    {
      HashMap<String, Integer> map = new HashMap<String, Integer>(columnSize);
      
      for(int j = 0; j < columnSize; j++)
      {
        map.put(c.getColumnName(j), new Integer(c.getInt(j)));
      }
      
      c.moveToNext();
      
      data.add(map);
    }
    
    return  data; 
  }
  
}

   
    
    
    
  










Related examples in the same category

1.Use SQLiteDatabase
2.SQLiteDatabase and ReentrantLock
3.SQLiteDatabase Helper class
4.Using Database
5.SQLite based diary app
6.Create table, insert record, delete records, query table, remove table
7.Insert Data into database
8.Searchable Dictionary
9.Sqlite Annotations Helper
10.Convert From Java Week To Sqlite Week