Single-instance database classes in Android projects to solve the locked

Source: Internet
Author: User

first, the database operation Packagecom.ping.db;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;/*** @describe < database operations >*/ Public classSqlitetoolextendsSqliteopenhelper {Private Static FinalString db_name = "medicalscience.db";//Database file name Private Static Final intVERSION = 1;//Database Version    Public StaticSqlitetool newinstance (context context) {return NewSqlitetool (context, db_name,NULL, VERSION); } PrivateSqlitetool (context context, String name, Cursorfactory factory,intversion) {  Super(context, name, Factory, version);} @Override Public voidonCreate (Sqlitedatabase db) {Db.execsql (Dbtcollect.createtablesql ());//when you need to create a table, you can add a} @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intNewVersion) {//called when the database version becomes largeronCreate (db);}} Second, the database management class Packagecom.ping.db;ImportJava.util.concurrent.atomic.AtomicInteger;ImportAndroid.annotation.SuppressLint;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Build;Importcom.ping.MyApplication;/*** @describe < database management Category >*/ Public classDBManager {PrivateAtomicinteger Dbopencount =NewAtomicinteger ();//counter Private StaticDBManager instance;Private StaticSqlitetool Sqllitetool;Privatesqlitedatabase database; Public Static synchronizedDBManager getinstance () {if(Instance = =NULL) {   synchronized(DBManager.class) {    if(Instance = =NULL) {instance=NewDBManager (); }   }  }  returninstance;} PrivateDBManager () {Sqllitetool=sqlitetool.newinstance (Myapplication.getinstance (). Getapplicationcontext ());} @SuppressLint ("Newapi")  Public synchronizedsqlitedatabase OpenDatabase () {if(Dbopencount.incrementandget () = = 1) {Database=sqllitetool.getwritabledatabase (); if(Build.VERSION.SDK_INT >= 11) {database.enablewriteaheadlogging ();//allow read and write simultaneous   }  }  returndatabase;}  Public synchronized voidcloseDatabase () {if(Dbopencount.decrementandget () = = 0) {database.close (); Database=NULL; }} III, data additions and deletions to search Packagecom.ping.db;Importjava.util.ArrayList;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;/*** @describe < database operations--operation of the collection table >*/ Public classDbtcollect { Public Static FinalString tname = "Tcollect"; //to create a statement for a table  Public StaticString Createtablesql () {return"CREATE TABLE IF not EXISTS" + Tname + "(Kid INTEGER PRIMARY KEY autoincrement, id text, title text)"; } //to delete a table statement  Public StaticString Deletetabelsql () {return"DROP TABLE IF EXISTS" +Tname;} //querying data for existence  Public Static Booleaniscollected (String id) {Booleaniscollected =false; Sqlitedatabase DB= Dbmanager.getinstance (). OpenDatabase ();//Get Sqlitedatabase ObjectString 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  returniscollected;} //Delete Data  Public Static voidDeletecollect (String mid) {Sqlitedatabase db=dbmanager.getinstance (). OpenDatabase (); Db.delete (Tname,"id =?",Newstring[] {ID}); Dbmanager.getinstance (). CloseDatabase (); }     //Add Data  Public Static voidaddcollect (Object obj) {sqlitedatabase db=dbmanager.getinstance (). OpenDatabase (); Contentvalues CV=Newcontentvalues (); Cv.put ("id", obj.id); Cv.put ("Title", Obj.title); Db.insert (Tname,NULL, CV); Dbmanager.getinstance (). CloseDatabase (); } //Querying Data  Public StaticArraylist<object>Getallcollect () {Sqlitedatabase db=dbmanager.getinstance (). OpenDatabase (); Cursor Cursor= Db.query (Tname,NULL,NULL,NULL,NULL,NULL,NULL); ArrayList<Object> OBJS =NewArraylist<object>();  while(Cursor.movetonext ()) {Object obj=NewObject (); Obj.id= Cursor.getstring (Cursor.getcolumnindex ("id")); Obj.title= Cursor.getstring (Cursor.getcolumnindex ("title"));  Objs.add (obj);  } cursor.close ();  Dbmanager.getinstance (). CloseDatabase (); returnObjs;}}

Single-instance database classes in Android projects to solve the locked

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.