Skip to content

Commit

Permalink
SQLiteOpenHelper refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cundong committed Apr 22, 2015
1 parent 4bc4dee commit 929887d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 74 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cundong.izhihu"
android:versionCode="22"
android:versionName="2.3.6" >
android:versionCode="23"
android:versionName="2.3.7" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
26 changes: 23 additions & 3 deletions src/com/cundong/izhihu/ZhihuApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;
import android.content.Context;

import com.cundong.izhihu.db.DatabaseHelper;
import com.cundong.izhihu.db.NewsDataSource;
import com.cundong.izhihu.db.NewsFavoriteDataSource;
import com.cundong.izhihu.db.NewsReadDataSource;
Expand All @@ -14,6 +15,9 @@
public class ZhihuApplication extends Application {

private static ZhihuApplication mApplication;

private DatabaseHelper mDatabaseHelper;

private static NewsDataSource mNewsDataSource;
private static NewsReadDataSource mNewsReadDataSource;
private static NewsFavoriteDataSource mNewsFavoriteDataSource;
Expand All @@ -24,13 +28,29 @@ public void onCreate() {
super.onCreate();

mApplication = this;
mNewsDataSource = new NewsDataSource(getApplicationContext());
mNewsReadDataSource = new NewsReadDataSource(getApplicationContext());
mNewsFavoriteDataSource = new NewsFavoriteDataSource(getApplicationContext());

mDatabaseHelper = DatabaseHelper.getInstance(getApplicationContext());

mNewsDataSource = new NewsDataSource(mDatabaseHelper);
mNewsReadDataSource = new NewsReadDataSource(mDatabaseHelper);
mNewsFavoriteDataSource = new NewsFavoriteDataSource(mDatabaseHelper);

initImageLoader(getApplicationContext());
}

@Override
public void onLowMemory() {
super.onLowMemory();
}


@Override
public void onTerminate() {
super.onTerminate();

mDatabaseHelper.close();
}

public static ZhihuApplication getInstance() {
return mApplication;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public final class DBHelper extends SQLiteOpenHelper {
public final class DatabaseHelper extends SQLiteOpenHelper {

//db
public static final String DB_NAME = "news_paper.db";
Expand Down Expand Up @@ -48,10 +48,20 @@ public final class DBHelper extends SQLiteOpenHelper {
+ FAVORITE_COLUMN_NEWS_LOGO + " CHAR(1024), "
+ FAVORITE_COLUMN_NEWS_SHARE_URL + " CHAR(1024));";

public DBHelper(Context context) {
private static DatabaseHelper mDBHelper;

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

public static synchronized DatabaseHelper getInstance(Context context) {
if (mDBHelper == null) {
mDBHelper = new DatabaseHelper(context);
}

return mDBHelper;
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(NEWS_TABLE_CREATE);
Expand Down
58 changes: 28 additions & 30 deletions src/com/cundong/izhihu/db/NewsDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
Expand All @@ -22,38 +21,37 @@
public final class NewsDataSource extends BaseDataSource {

private SQLiteDatabase database;
private DBHelper dbHelper;

private String[] allColumns = {
DBHelper.NEWS_COLUMN_ID,
DBHelper.NEWS_COLUMN_TYPE,
DBHelper.NEWS_COLUMN_KEY,
DBHelper.NEWS_COLUMN_CONTENT };
DatabaseHelper.NEWS_COLUMN_ID,
DatabaseHelper.NEWS_COLUMN_TYPE,
DatabaseHelper.NEWS_COLUMN_KEY,
DatabaseHelper.NEWS_COLUMN_CONTENT };

public NewsDataSource(Context context) {
dbHelper = new DBHelper(context);
public NewsDataSource(DatabaseHelper dbHelper) {
database = dbHelper.getWritableDatabase();
}

private ArrayList<NewsEntity> insertDailyNewsList(int type, String key, String content) {
ContentValues values = new ContentValues();
values.put(DBHelper.NEWS_COLUMN_TYPE, type);
values.put(DBHelper.NEWS_COLUMN_KEY, key);
values.put(DBHelper.NEWS_COLUMN_CONTENT, content);
values.put(DatabaseHelper.NEWS_COLUMN_TYPE, type);
values.put(DatabaseHelper.NEWS_COLUMN_KEY, key);
values.put(DatabaseHelper.NEWS_COLUMN_CONTENT, content);

long insertId = database.insert(DBHelper.NEWS_TABLE_NAME, null, values);
Cursor cursor = database.query(DBHelper.NEWS_TABLE_NAME, allColumns,
DBHelper.NEWS_COLUMN_ID + " = " + insertId, null, null, null, null);
long insertId = database.insert(DatabaseHelper.NEWS_TABLE_NAME, null, values);
Cursor cursor = database.query(DatabaseHelper.NEWS_TABLE_NAME, allColumns,
DatabaseHelper.NEWS_COLUMN_ID + " = " + insertId, null, null, null, null);
ArrayList<NewsEntity> newsList = cursorToNewsList(cursor);
cursor.close();
return newsList;
}

private void updateNewsList(int type, String key, String content) {
ContentValues values = new ContentValues();
values.put(DBHelper.NEWS_COLUMN_TYPE, type);
values.put(DBHelper.NEWS_COLUMN_KEY, key);
values.put(DBHelper.NEWS_COLUMN_CONTENT, content);
database.update(DBHelper.NEWS_TABLE_NAME, values, DBHelper.NEWS_COLUMN_KEY
values.put(DatabaseHelper.NEWS_COLUMN_TYPE, type);
values.put(DatabaseHelper.NEWS_COLUMN_KEY, key);
values.put(DatabaseHelper.NEWS_COLUMN_CONTENT, content);
database.update(DatabaseHelper.NEWS_TABLE_NAME, values, DatabaseHelper.NEWS_COLUMN_KEY
+ "='" + key + "'", null);
}

Expand All @@ -72,11 +70,11 @@ private boolean isContentExist(String key) {

boolean result = false;

Cursor cursor = database.query(DBHelper.NEWS_TABLE_NAME, allColumns,
DBHelper.NEWS_COLUMN_KEY + " = '" + key + "'", null, null, null, null);
Cursor cursor = database.query(DatabaseHelper.NEWS_TABLE_NAME, allColumns,
DatabaseHelper.NEWS_COLUMN_KEY + " = '" + key + "'", null, null, null, null);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
String content = cursor.getString(cursor.getColumnIndex(DBHelper.NEWS_COLUMN_CONTENT));
String content = cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_COLUMN_CONTENT));
result = !TextUtils.isEmpty(content);
}

Expand All @@ -85,12 +83,12 @@ private boolean isContentExist(String key) {
}

public String getContent(String key) {
Cursor cursor = database.query(DBHelper.NEWS_TABLE_NAME, allColumns,
DBHelper.NEWS_COLUMN_KEY + " = '" + key + "'", null, null, null,
Cursor cursor = database.query(DatabaseHelper.NEWS_TABLE_NAME, allColumns,
DatabaseHelper.NEWS_COLUMN_KEY + " = '" + key + "'", null, null, null,
null);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
String content = cursor.getString(cursor.getColumnIndex(DBHelper.NEWS_COLUMN_CONTENT));
String content = cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_COLUMN_CONTENT));
cursor.close();
return content;
} else {
Expand All @@ -99,8 +97,8 @@ public String getContent(String key) {
}

public ArrayList<NewsEntity> getNewsList(String key) {
Cursor cursor = database.query(DBHelper.NEWS_TABLE_NAME, allColumns,
DBHelper.NEWS_COLUMN_KEY + " = '" + key + "'", null, null, null,
Cursor cursor = database.query(DatabaseHelper.NEWS_TABLE_NAME, allColumns,
DatabaseHelper.NEWS_COLUMN_KEY + " = '" + key + "'", null, null, null,
null);

ArrayList<NewsEntity> newsList = cursorToNewsList(cursor);
Expand All @@ -111,7 +109,7 @@ public ArrayList<NewsEntity> getNewsList(String key) {

private ArrayList<NewsEntity> cursorToNewsList(Cursor cursor) {
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
String content = cursor.getString(cursor.getColumnIndex(DBHelper.NEWS_COLUMN_CONTENT));
String content = cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_COLUMN_CONTENT));
return GsonUtils.getNewsList(content);
} else {
return null;
Expand All @@ -127,12 +125,12 @@ public NewsListEntity getLatestNews() {

NewsListEntity newsListEntity = null;

String orderBy = DBHelper.NEWS_COLUMN_KEY + " desc";
String orderBy = DatabaseHelper.NEWS_COLUMN_KEY + " desc";

Cursor cursor = database.query(DBHelper.NEWS_TABLE_NAME, allColumns, DBHelper.NEWS_COLUMN_TYPE + "=" + Constants.NEWS_LIST, null, null, null, orderBy);
Cursor cursor = database.query(DatabaseHelper.NEWS_TABLE_NAME, allColumns, DatabaseHelper.NEWS_COLUMN_TYPE + "=" + Constants.NEWS_LIST, null, null, null, orderBy);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
String content = cursor.getString(cursor.getColumnIndex(DBHelper.NEWS_COLUMN_CONTENT));
String content = cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_COLUMN_CONTENT));
newsListEntity = (NewsListEntity) GsonUtils.getEntity(content, NewsListEntity.class);
}

Expand Down
47 changes: 22 additions & 25 deletions src/com/cundong/izhihu/db/NewsFavoriteDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
Expand All @@ -19,29 +18,27 @@
public final class NewsFavoriteDataSource extends BaseDataSource {

private SQLiteDatabase database;
private DBHelper dbHelper;

private String[] allColumns = {
DBHelper.FAVORITE_COLUMN_ID,
DBHelper.FAVORITE_COLUMN_NEWS_ID,
DBHelper.FAVORITE_COLUMN_NEWS_TITLE,
DBHelper.FAVORITE_COLUMN_NEWS_LOGO,
DBHelper.FAVORITE_COLUMN_NEWS_SHARE_URL
DatabaseHelper.FAVORITE_COLUMN_ID,
DatabaseHelper.FAVORITE_COLUMN_NEWS_ID,
DatabaseHelper.FAVORITE_COLUMN_NEWS_TITLE,
DatabaseHelper.FAVORITE_COLUMN_NEWS_LOGO,
DatabaseHelper.FAVORITE_COLUMN_NEWS_SHARE_URL
};

public NewsFavoriteDataSource(Context context) {
dbHelper = new DBHelper(context);
public NewsFavoriteDataSource(DatabaseHelper dbHelper) {
database = dbHelper.getWritableDatabase();
}

private void insert(String newsId, String newsTitle, String newsLogo, String newsShareUrl) {
ContentValues values = new ContentValues();
values.put(DBHelper.FAVORITE_COLUMN_NEWS_ID, newsId);
values.put(DBHelper.FAVORITE_COLUMN_NEWS_TITLE, newsTitle);
values.put(DBHelper.FAVORITE_COLUMN_NEWS_LOGO, newsLogo);
values.put(DBHelper.FAVORITE_COLUMN_NEWS_SHARE_URL, newsShareUrl);
values.put(DatabaseHelper.FAVORITE_COLUMN_NEWS_ID, newsId);
values.put(DatabaseHelper.FAVORITE_COLUMN_NEWS_TITLE, newsTitle);
values.put(DatabaseHelper.FAVORITE_COLUMN_NEWS_LOGO, newsLogo);
values.put(DatabaseHelper.FAVORITE_COLUMN_NEWS_SHARE_URL, newsShareUrl);

database.insert(DBHelper.FAVORITE_TABLE_NAME, null, values);
database.insert(DatabaseHelper.FAVORITE_TABLE_NAME, null, values);
}

/**
Expand Down Expand Up @@ -75,8 +72,8 @@ public boolean isInFavorite(String newsId) {

boolean result = false;

Cursor cursor = database.query(DBHelper.FAVORITE_TABLE_NAME, allColumns,
DBHelper.FAVORITE_COLUMN_NEWS_ID + " = '" + newsId + "'", null,
Cursor cursor = database.query(DatabaseHelper.FAVORITE_TABLE_NAME, allColumns,
DatabaseHelper.FAVORITE_COLUMN_NEWS_ID + " = '" + newsId + "'", null,
null, null, null);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
Expand All @@ -96,9 +93,9 @@ public ArrayList<NewsEntity> getFavoriteList() {

ArrayList<NewsEntity> newsList = new ArrayList<NewsEntity>();

String orderBy = DBHelper.FAVORITE_COLUMN_ID + " DESC";
String orderBy = DatabaseHelper.FAVORITE_COLUMN_ID + " DESC";

Cursor cursor = database.query(DBHelper.FAVORITE_TABLE_NAME, allColumns,
Cursor cursor = database.query(DatabaseHelper.FAVORITE_TABLE_NAME, allColumns,
null, null, null, null, orderBy);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
Expand All @@ -107,10 +104,10 @@ public ArrayList<NewsEntity> getFavoriteList() {

NewsEntity newsEntity = new NewsEntity();

String newsId = cursor.getString(cursor.getColumnIndex(DBHelper.FAVORITE_COLUMN_NEWS_ID));
String newsTitle = cursor.getString(cursor.getColumnIndex(DBHelper.FAVORITE_COLUMN_NEWS_TITLE));
String newsLogo = cursor.getString(cursor.getColumnIndex(DBHelper.FAVORITE_COLUMN_NEWS_LOGO));
String newsShareUrl = cursor.getString(cursor.getColumnIndex(DBHelper.FAVORITE_COLUMN_NEWS_SHARE_URL));
String newsId = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FAVORITE_COLUMN_NEWS_ID));
String newsTitle = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FAVORITE_COLUMN_NEWS_TITLE));
String newsLogo = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FAVORITE_COLUMN_NEWS_LOGO));
String newsShareUrl = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FAVORITE_COLUMN_NEWS_SHARE_URL));

newsEntity.id = Long.parseLong(newsId);
newsEntity.title = newsTitle;
Expand Down Expand Up @@ -138,15 +135,15 @@ public ArrayList<NewsEntity> getFavoriteList() {

public void deleteFromFavorite(String newsId) {

String whereClause = DBHelper.FAVORITE_COLUMN_NEWS_ID + "=?";
String whereClause = DatabaseHelper.FAVORITE_COLUMN_NEWS_ID + "=?";

String [] whereArgs = { String.valueOf(newsId) };

database.delete(DBHelper.FAVORITE_TABLE_NAME, whereClause, whereArgs);
database.delete(DatabaseHelper.FAVORITE_TABLE_NAME, whereClause, whereArgs);
}

public void deleteFromFavorite() {

database.delete(DBHelper.FAVORITE_TABLE_NAME, null, null);
database.delete(DatabaseHelper.FAVORITE_TABLE_NAME, null, null);
}
}
22 changes: 10 additions & 12 deletions src/com/cundong/izhihu/db/NewsReadDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
Expand All @@ -17,19 +16,18 @@
public final class NewsReadDataSource extends BaseDataSource {

private SQLiteDatabase database;
private DBHelper dbHelper;
private String[] allColumns = { DBHelper.READ_COLUMN_ID,
DBHelper.READ_COLUMN_NEWSID };

public NewsReadDataSource(Context context) {
dbHelper = new DBHelper(context);
private String[] allColumns = { DatabaseHelper.READ_COLUMN_ID,
DatabaseHelper.READ_COLUMN_NEWSID };

public NewsReadDataSource(DatabaseHelper dbHelper) {
database = dbHelper.getWritableDatabase();
}

private void insert(String newsId) {
ContentValues values = new ContentValues();
values.put(DBHelper.READ_COLUMN_NEWSID, newsId);
database.insert(DBHelper.READ_TABLE_NAME, null, values);
values.put(DatabaseHelper.READ_COLUMN_NEWSID, newsId);
database.insert(DatabaseHelper.READ_TABLE_NAME, null, values);
}

/**
Expand Down Expand Up @@ -60,8 +58,8 @@ private boolean isNewsRead(String newsId) {

boolean result = false;

Cursor cursor = database.query(DBHelper.READ_TABLE_NAME, allColumns,
DBHelper.READ_COLUMN_NEWSID + " = '" + newsId + "'", null,
Cursor cursor = database.query(DatabaseHelper.READ_TABLE_NAME, allColumns,
DatabaseHelper.READ_COLUMN_NEWSID + " = '" + newsId + "'", null,
null, null, null);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
Expand All @@ -81,13 +79,13 @@ public ArrayList<String> getNewsReadList() {

ArrayList<String> newsList = new ArrayList<String>();

Cursor cursor = database.query(DBHelper.READ_TABLE_NAME, allColumns,
Cursor cursor = database.query(DatabaseHelper.READ_TABLE_NAME, allColumns,
null, null, null, null, null);

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {

while (!cursor.isAfterLast()) {
String newsId = cursor.getString(cursor.getColumnIndex(DBHelper.READ_COLUMN_NEWSID));
String newsId = cursor.getString(cursor.getColumnIndex(DatabaseHelper.READ_COLUMN_NEWSID));
newsList.add(newsId);

cursor.moveToNext();
Expand Down

0 comments on commit 929887d

Please sign in to comment.