EatDBHelper.java :  » Location » androideatthis » com » sopt » eat » db » Android Open Source

Android Open Source » Location » androideatthis 
androideatthis » com » sopt » eat » db » EatDBHelper.java
package com.sopt.eat.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.DateUtils;
import android.util.Log;

import com.sopt.eat.db.Constants.EatDB;
import com.sopt.eat.vo.SearchItem;

public class EatDBHelper extends SQLiteOpenHelper {

  static final String TAG = "changdoc";

  public EatDBHelper(Context c) {
    super(c, EatDB.DB_NAME, null, EatDB.DB_VERSION);

  }

  @Override
  public void onCreate(SQLiteDatabase db) {

    String curSql = "CREATE TABLE " + EatDB.TABLE_HISTORY + " ("
        + EatDB.HISTORY_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + EatDB.HISTORY_LAT + " text," + EatDB.HISTORY_LOT + " text,"
        + EatDB.HISTORY_PHONE + " text," + EatDB.HISTORY_TITLE
        + " text," + EatDB.HISTORY_REGDATE_LONG + " text" + " );";

    db.execSQL(curSql);

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.v("changdoc", "DATABASE RESET TO version:" + newVersion);

    db.execSQL("DROP TABLE " + EatDB.TABLE_HISTORY);

    onCreate(db);
  }

  public ArrayList<SearchItem> getHistoryList() {
    ArrayList<SearchItem> result = new ArrayList<SearchItem>();
    SQLiteDatabase sdb = this.getReadableDatabase();

    Cursor cursor = sdb.query(EatDB.TABLE_HISTORY, null, null, null, null,
        null, EatDB.HISTORY_SEQ + " DESC");

    while (cursor.moveToNext()) {
      SearchItem curItem = new SearchItem();

      curItem.setSeq(cursor.getInt(cursor
          .getColumnIndex(EatDB.HISTORY_SEQ)));
      curItem.setTitle(cursor.getString(cursor
          .getColumnIndex(EatDB.HISTORY_TITLE)));

      curItem.setLatitude(cursor.getDouble(cursor
          .getColumnIndex(EatDB.HISTORY_LAT)));
      curItem.setLongitude(cursor.getDouble(cursor
          .getColumnIndex(EatDB.HISTORY_LOT)));

      curItem.setPhonenumber(cursor.getString(cursor
          .getColumnIndex(EatDB.HISTORY_PHONE)));
      Long curDate = cursor.getLong(cursor
          .getColumnIndex(EatDB.HISTORY_REGDATE_LONG));
      curItem.setRegdate(DateUtils.getRelativeTimeSpanString(curDate)
          .toString());

      result.add(curItem);

    }
    cursor.close();

    sdb.close();
    return result;
  }

  public boolean deleteBySeq(int seq) {

    SQLiteDatabase sdb = this.getWritableDatabase();
    int res = sdb.delete(EatDB.TABLE_HISTORY, EatDB.HISTORY_SEQ + " = "+seq, null);
    
    sdb.close();
    
    if(res>0)return true;
    else return false;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.