Android项目中单实例数据库类,解决database is locked

时间:2022-12-08 18:21:37
一、数据库操作
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 <数据库操作>
 */
public class SQLiteTool extends SQLiteOpenHelper {
 private static final String DB_NAME = "medicalscience.db"; // 数据库文件名
 private static final int VERSION = 1;// 数据库版本
 
 
 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());//有需要创建表时,可以在这添加
 }
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//当数据库版本变大时调用
  onCreate(db);
 }
}
 
二、数据库管理类
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 <数据库管理类>
 */
public class DBManager {
 private AtomicInteger dbOpenCount = new AtomicInteger();//计数器
 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();// 允许读写同时进行
   }
  }
  return database;
 }
 public synchronized void closeDatabase() {
  if (dbOpenCount.decrementAndGet() == 0) {
   database.close();
   database = null;
  }
 }
}
 
三、数据增删改查
package com.ping.db;
import java.util.ArrayList;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
 * @describe <数据库操作--收藏表的操作 >
 */
public class DBTCollect {
 public static final String TNAME = "Tcollect";
 // 创建表的语句
 public static String createTableSQL() {
  return "CREATE TABLE IF NOT EXISTS " + TNAME + " (kid INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT, title TEXT)";
 }
 // 删除表的语句
 public static String deleteTabelSQL() {
  return "DROP TABLE IF EXISTS " + TNAME;
 }
 //查询数据是否存在
 public static boolean isCollected(String id) {
  boolean isCollected = false;
  SQLiteDatabase db = DBManager.getInstance().openDatabase();//获得SQLiteDatabase对象
  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();//关闭
  return isCollected;
 }
 
    // 删除数据
 public static void deleteCollect(String mid) {
  SQLiteDatabase db = DBManager.getInstance().openDatabase();
  db.delete(TNAME, "id = ?", new String[] { id });
  DBManager.getInstance().closeDatabase();
 }
    
 //增加数据
 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();
 }
 //查询数据
 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;
 }
}