Single-instance database classes in Android projects

Source: Internet
Author: User

First, the database operation

Package com.ping.db;

Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;
Import Android.database.sqlite.SQLiteOpenHelper;

/**
* @describe < database operations >
*/
public class Sqlitetool extends Sqliteopenhelper {
private static final String db_name = "medicalscience.db"; Database file name
private static final int version = 1;//database versions


public static Sqlitetool newinstance (context context) {
return new Sqlitetool (context, db_name, NULL, VERSION);
}

Private Sqlitetool (context context, String name, Cursorfactory factory, int version) {
Super (context, name, Factory, version);
}

@Override
public void OnCreate (Sqlitedatabase db) {
Db.execsql (Dbtcollect.createtablesql ());//When you need to create a table, you can add
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//called when the database version becomes larger
OnCreate (DB);
}
}

Second, the database management class

Package com.ping.db;

Import Java.util.concurrent.atomic.AtomicInteger;

Import Android.annotation.SuppressLint;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.os.Build;

Import com.ping.MyApplication;

/**
* @describe < database management Category >
*/
public class DBManager {
Private Atomicinteger Dbopencount = new Atomicinteger ();//Counter
private static DBManager instance;
private static Sqlitetool Sqllitetool;
Private Sqlitedatabase database;

public static synchronized DBManager getinstance () {
if (instance = = null) {
Synchronized (Dbmanager.class) {
if (instance = = null) {
Instance = new DBManager ();
}
}
}
return instance;
}

Private DBManager () {
Sqllitetool = Sqlitetool.newinstance (Myapplication.getinstance (). Getapplicationcontext ());
}

@SuppressLint ("Newapi")
Public synchronized Sqlitedatabase OpenDatabase () {
if (dbopencount.incrementandget () = = 1) {
Database = Sqllitetool.getwritabledatabase ();
if (Build.VERSION.SDK_INT >= 11) {
Database.enablewriteaheadlogging ();//allow simultaneous reading and writing
}
}
return database;
}

Public synchronized void CloseDatabase () {
if (dbopencount.decrementandget () = = 0) {
Database.close ();
Database = null;
}
}
}

Third, data additions and deletions to check

Package com.ping.db;

Import java.util.ArrayList;

Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;

/**
* @describe < Database operations--operation of the collection table >
*/
public class Dbtcollect {
public static final String tname = "Tcollect";

To create a statement for a table
public static String Createtablesql () {
Return "CREATE TABLE IF not EXISTS" + Tname + "(Kid INTEGER PRIMARY KEY autoincrement, id text, title text)";
}

To delete a table statement
public static String Deletetabelsql () {
Return "DROP TABLE IF EXISTS" + tname;
}

Querying data for existence
public static Boolean iscollected (String ID) {
Boolean iscollected = false;
Sqlitedatabase db = Dbmanager.getinstance (). OpenDatabase ();//Get Sqlitedatabase Object
String sql = "SELECT * from" + Tname + "WHERE id = '" + ID + "'";
cursor cursor = db.rawquery (sql, NULL);
if (Cursor.getcount () > 0) {
Iscollected = true;
}
Cursor.close ();
Dbmanager.getinstance (). CloseDatabase ();//Close
return iscollected;
}

Delete data
public static void Deletecollect (String mid) {
Sqlitedatabase db = Dbmanager.getinstance (). OpenDatabase ();
Db.delete (tname, "id =?", new string[] {ID});
Dbmanager.getinstance (). CloseDatabase ();
}

Add data
public static void Addcollect (Object obj) {
Sqlitedatabase db = Dbmanager.getinstance (). OpenDatabase ();
Contentvalues CV = new Contentvalues ();
Cv.put ("id", obj.id);
Cv.put ("title", Obj.title);
Db.insert (tname, NULL, CV);
Dbmanager.getinstance (). CloseDatabase ();
}

Querying data
public static arraylist<object> Getallcollect () {
Sqlitedatabase db = Dbmanager.getinstance (). OpenDatabase ();
cursor cursor = db.query (tname, NULL, NULL, NULL, NULL, NULL, NULL);
arraylist<object> Objs = new arraylist<object> ();
while (Cursor.movetonext ()) {
Object obj = new Object ();
Obj.id = cursor.getstring (Cursor.getcolumnindex ("id"));
Obj.title = cursor.getstring (Cursor.getcolumnindex ("title"));
Objs.add (obj);
}
Cursor.close ();
Dbmanager.getinstance (). CloseDatabase ();
return OBJS;
}
}

Single-instance database classes in Android projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.