Android Open Source - blogwriter D B Helper






From Project

Back to project page blogwriter.

License

The source code is released under:

GNU General Public License

If you think the Android project blogwriter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.blogerwriter;
//from w  w  w.j a  va2  s.  c  o  m
import java.text.SimpleDateFormat;
import java.util.Date;

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.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper {

//  private DBHelper mInstance = null;

  // ??????log
  private static final String TAG = "DBopenHelper";

  // ???????????????
  public static final String KEY_ID = "_id";

  // ?????????????
  public static final String KEY_DATA = "data";
  // ???????????id
  public static final String KEY_DATE = "date";

  // ??????????data
  private static final String DB_NAME = "blogwriter.db";

  // ?????????
  private static final String DB_TABLE = "isaid";

  // ???????
  private static final int DB_VERSION = 1;

  // ??Context??
  private Context mContext = null;

  // ?????
  private static final String DB_CREATE = "CREATE TABLE " + DB_TABLE + " ("
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATA + " TEXT," + KEY_DATE
      + " TEXT)";

  // ??open????????????????????????
  private SQLiteDatabase mSQLiteDatabase = null;

  // ?SQLiteOpenHelper??????
  private DatabaseHelper mDatabaseHelper = null;

  private static class DatabaseHelper extends SQLiteOpenHelper {

    /* ????-????????? */
    DatabaseHelper(Context context) {

      // ???getWritableDatabase()
      // ? getReadableDatabase()???
      // ??????????
      super(context, DB_NAME, null, DB_VERSION);
    }

    /* ????? */
    @Override
    public void onCreate(SQLiteDatabase db) {

      // ?????????????
      db.execSQL(DB_CREATE);
    }

    /* ????????? */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

      db.execSQL("DROP TABLE IF EXISTS notes");
      onCreate(db);
    }
  }

  public DBHelper(Context context) {
    mContext = context;
  }

//  public DBHelper getInstance(Context context){
//    if(mInstance==null){
//      mInstance=new DBHelper(context);
//    }
//    return mInstance;
//  }
  // ?????????????????
  public void open() {

    mDatabaseHelper = new DatabaseHelper(mContext);
    mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
  }

  // ???????
  public void close() {

    mDatabaseHelper.close();
  }

  /* ???????????? */
  public long insertData(String data, String date) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_DATA, data);
    initialValues.put(KEY_DATE, date);

    return mSQLiteDatabase.insert(DB_TABLE, KEY_ID, initialValues);
  }

  /* ?????????? */
  public boolean deleteData(long rowId) {

    return mSQLiteDatabase.delete(DB_TABLE, KEY_ID + "=" + rowId, null) > 0;
  }

  /* ??Cursor???????? */
  public Cursor fetchAllData() {

    return mSQLiteDatabase.query(DB_TABLE, new String[] { KEY_ID, KEY_DATA,
        KEY_DATE }, null, null, null, null, null);
  }

  /* ???????? */
  public Cursor fetchData(long rowId) throws SQLException {

    Cursor mCursor = mSQLiteDatabase.query(true, DB_TABLE, new String[] {
        KEY_ID, KEY_DATA, KEY_DATE }, KEY_ID + "=" + rowId, null, null,
        null, null, null);

    if (mCursor != null) {

      mCursor.moveToFirst();
    }
    return mCursor;

  }

  /* ?????????? */
  public boolean updateData(long rowId, String data, String date) {

    ContentValues args = new ContentValues();
    args.put(KEY_DATA, data);
    args.put(KEY_DATE, date);

    return mSQLiteDatabase.update(DB_TABLE, args, KEY_ID + "=" + rowId,
        null) > 0;
  }

  public String getTimeStr(long time) {
    Date d = new Date(time);
    // ?????????????????????
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String s = sdf.format(d);
    return s;
  }

  public String getAllDataString(){
    Cursor cur=fetchAllData();
    StringBuilder sb=new StringBuilder();
    for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()){
      int idata=cur.getColumnIndex(KEY_DATA);
      int idate=cur.getColumnIndex(KEY_DATE);
      String data=cur.getString(idata);
      String date=cur.getString(idate);
      sb.append(data);
      sb.append("\n");
      sb.append(date);
      sb.append("\n");
    }
//    do{
//    }while(c.moveToNext());
    return sb.toString();
  }
}




Java Source Code List

com.example.blogerwriter.DBHelper.java
com.example.blogerwriter.DataExporter.java
com.example.blogerwriter.ItemEditActivity.java
com.example.blogerwriter.MainActivity.java
com.example.blogerwriter.MemoActivity.java
com.example.blogerwriter.VersionTransfer.java