Skip to content

Instantly share code, notes, and snippets.

@riyazMuhammad
Last active October 16, 2019 02:36
Show Gist options
  • Save riyazMuhammad/8b384166947facc5a7104a676d90f1bc to your computer and use it in GitHub Desktop.
Save riyazMuhammad/8b384166947facc5a7104a676d90f1bc to your computer and use it in GitHub Desktop.
/**
* Database helper. All database (sqlite) calls can go in here
*
* @author Muhammad Riyaz
*
*/
public class DBHelper {
private static final String DB_NAME = "db_name";
private static final int DB_VER = 1;
private static final String TAG = DBhelper.class.getSimpleName();
// table names
private static final String TABLE_POSTS = "posts";
// field names.
public static final String C_POST_ID = "post_id";
public static final String C_POST_URL = "url";
public static final String C_POST_TITLE = "title";
public static final String C_POST_CONTENT = "content";
// Initializations
private static DBOpenHelper ourHelper;
private static SQLiteDatabase ourDataBase;
private int openCounter = 0;
private static DBhelper ourInstance;
public static class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create table queries
String sql_posts = String
.format("create TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT,%s TEXT," +
"%s TEXT)",
TABLE_POSTS, C_POST_ID, C_POST_TITLE,
C_POST_URL, C_POST_CONTENT);
Log.d(TAG, "onCreate sql: " + sql_posts);
db.execSQL(sql_posts);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// write here the queries to be executed when the DB_VER upgrades
db.execSQL("drop table if exists " + TABLE_POSTS);
Log.d(TAG, "On upgrade destroyed table " + TABLE_POSTS);
this.onCreate(db);
}
}
public static synchronized void initDB(Context context) {
if (ourInstance == null) {
ourInstance = new DBhelper();
ourHelper = new DBOpenHelper(context);
}
}
public static synchronized DBhelper getInstance() {
if (ourInstance == null) {
throw new IllegalStateException(
DBhelper.class.getSimpleName()
+ " is not initialized, call initializeInstance(..) method first.");
}
return ourInstance;
}
public synchronized DBhelper writeOpen() {
openCounter++;
if (openCounter == 1) {
Log.i(TAG, "new write open");
ourDataBase = ourHelper.getWritableDatabase();
} else {
Log.i(TAG, "old write open");
}
return this;
}
public synchronized DBhelper readOpen() {
openCounter++;
if (openCounter == 1) {
Log.i(TAG, "new read open");
ourDataBase = ourHelper.getReadableDatabase();
} else {
Log.i(TAG, "old read open");
}
return this;
}
public synchronized void closeDatabase() {
openCounter--;
if (openCounter == 0) {
Log.i(TAG, "closing db");
// Closing database
ourDataBase.close();
// ourDataBase.releaseReference();
} else {
Log.i(TAG, "not closing db. Open connections: " + openCounter);
}
}
public ArrayList<Post> getPosts(){
ArrayList<Post> postsArrayList = new ArrayList<>();
Cursor posts = ourDataBase.query(TABLE_1, null, null, null, null,
null, null);
if (posts != null && posts.getCount() > 0) {
for (int i = 0; i < posts.getCount(); i++) {
posts.moveToPosition(i);
postsArrayList.add(new Post(
posts.getLong(posts.getColumnIndex(DBHelper.C_POST_ID)),
posts.getString(posts.getColumnIndex(DBHelper.C_POST_TITLE)),
posts.getString(posts.getColumnIndex(DBHelper.C_POST_CONTENT)),
posts.getString(posts.getColumnIndex(DBHelper.C_POST_URL))));
}
return postsArrayList;
} else {
return null;
}
}
public long insertPost(long postID, String postTitle,
String postURL, String postContent) {
ContentValues cv = new ContentValues();
cv.put(C_POST_ID, postID);
cv.put(C_POST_TITLE, postTitle);
cv.put(C_POST_URL, postURL);
cv.put(C_POST_CONTENT, postContent);
Log.i(TAG, "inserted post " + postID + " title: " + postTitle);
return ourDataBase.insert(TABLE_POSTS, null, cv);
}
}
public class MyActivity extends AppCompatActivity {
private static final String TAG = MyActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
// reading from DB
DBHelper helper = DBHelper.getInstance();
helper.readOpen();
ArrayList<Post> posts = helper.getPosts();
helper.closeDatabase();
// wrinting to DB
DBHelper helper = DBHelper.getInstance();
helper.writeOpen();
long postInsertedID = helper.insertPost(1,"title","https://urlofthepost","content of the post");
helper.closeDatabase();
}
}
/**
* Created by muhammadriyaz
*/
public class MyApplication extends Application {
private static final String TAG = MyApplication.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
// open db instance
DBHelper.initDB(this);
}
}
public class Post {
private String title, postUrl, content;
long id;
/**
*
* @param id
* @param title
* @param content
* @param postUrl
*/
public Post(long id, String title, String content, String postUrl) {
this.id = id;
this.title = title;
this.content = content;
this.postUrl = postUrl;
}
public String getTitle() {
return title;
}
public long getID() {
return id;
}
public String getContent() {
return content;
}
public String getPostUrl() {
return postUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment