android sqlite synchronized,Android上SQLite的最佳实践是什么?

Thread或

AsyncTask用于长期运行的操作(50 ms+)。测试你的应用程序,看看它在哪里。大多数操作(可能)不需要线程,因为大多数操作(可能)只涉及几行。使用线程进行批量操作。

共享一

SQLiteDatabase实例,用于磁盘上线程之间的每个DB,并实现一个计数系统以跟踪打开的连接。对于这些场景有什么最佳实践吗?

在所有类之间共享一个静态字段。我过去常常留着一个单身的人来做那些和其他需要分享的事情。还应该使用计数方案(通常使用AtomicInteger)来确保您从未过早关闭数据库或使其处于打开状态。我的解决方案:

有关最新版本,请参见https://github.com/JakarCo/databasemanager但我也会尽量保持代码的最新。如果您想了解我的解决方案,请看代码并阅读我的说明。我的笔记通常很有用。将代码复制/粘贴到名为

DatabaseManager..(或从GitHub下载)

延展

DatabaseManager和实施

onCreate和

onUpgrade就像你平时一样。您可以创建一个子类的多个子类。

DatabaseManager类以使磁盘上有不同的数据库。

实例化子类并调用

getDb()使用

SQLiteDatabase班级,等级。

打电话

close()对于您实例化的每个子类

密码复制/粘贴:import android.content.Context;import android.database.sqlite.SQLiteDatabase;import java.util.concurrent.ConcurrentHashMap;

/** Extend this class and use it as an SQLiteOpenHelper class

*

* DO NOT distribute, sell, or present this code as your own.

* for any distributing/selling, or whatever, see the info at the link below

*

* Distribution, attribution, legal stuff,

* See https://github.com/JakarCo/databasemanager

*

* If you ever need help with this code, contact me at support@androidsqlitelibrary.com (or support@jakar.co )

*

* Do not sell this. but use it as much as you want. There are no implied or express warranties with this code.

*

* This is a simple database manager class which makes threading/synchronization super easy.

*

* Extend this class and use it like an SQLiteOpenHelper, but use it as follows:

*  Instantiate this class once in each thread that uses the database.

*  Make sure to call {@link #close()} on every opened instance of this class

*  If it is closed, then call {@link #open()} before using again.

*

* Call {@link #getDb()} to get an instance of the underlying SQLiteDatabse class (which is synchronized)

*

* I also implement this system (well, it's very similar) in my Android SQLite Libray

at http://androidslitelibrary.com

*

*

*/abstract public class DatabaseManager {

/**See SQLiteOpenHelper documentation

*/

abstract public void onCreate(SQLiteDatabase db);

/**See SQLiteOpenHelper documentation

*/

abstract public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);

/**Optional.

* *

*/

public void onOpen(SQLiteDatabase db){}

/**Optional.

*

*/

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

/**Optional

*

*/

public void onConfigure(SQLiteDatabase db){}

/** The SQLiteOpenHelper class is not actually used by your application.

*

*/

static private class DBSQLiteOpenHelper extends SQLiteOpenHelper {

DatabaseManager databaseManager;

private AtomicInteger counter = new AtomicInteger(0);

public DBSQLiteOpenHelper(Context context, String name, int version, DatabaseManager databaseManager) {

super(context, name, null, version);

this.databaseManager = databaseManager;

}

public void addConnection(){

counter.incrementAndGet();

}

public void removeConnection(){

counter.decrementAndGet();

}

public int getCounter() {

return counter.get();

}

@Override        public void onCreate(SQLiteDatabase db) {

databaseManager.onCreate(db);

}

@Override        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

databaseManager.onUpgrade(db, oldVersion, newVersion);

}

@Override        public void onOpen(SQLiteDatabase db) {

databaseManager.onOpen(db);

}

@Override        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {

databaseManager.onDowngrade(db, oldVersion, newVersion);

}

@Override        public void onConfigure(SQLiteDatabase db) {

databaseManager.onConfigure(db);

}

}

private static final ConcurrentHashMap dbMap = new ConcurrentHashMap();

private static final Object lockObject = new Object();

private DBSQLiteOpenHelper sqLiteOpenHelper;

private SQLiteDatabase db;

private Context context;

/** Instantiate a new DB Helper.


 SQLiteOpenHelpers are statically cached so they (and their internally cached SQLiteDatabases) will be reused for concurrency

*

* @param context Any {@link android.content.Context} belonging to your package.

* @param name The database name. This may be anything you like. Adding a file extension is not required and any

file extension you would like to use is fine.

* @param version the database version.

*/

public DatabaseManager(Context context, String name, int version) {

String dbPath = context.getApplicationContext().getDatabasePath(name).getAbsolutePath();

synchronized (lockObject) {

sqLiteOpenHelper = dbMap.get(dbPath);

if (sqLiteOpenHelper==null) {

sqLiteOpenHelper = new DBSQLiteOpenHelper(context, name, version, this);

dbMap.put(dbPath,sqLiteOpenHelper);

}

//SQLiteOpenHelper class caches the SQLiteDatabase, so this will be the same SQLiteDatabase object every time

db = sqLiteOpenHelper.getWritableDatabase();

}

this.context = context.getApplicationContext();

}

/**Get the writable SQLiteDatabase

*/

public SQLiteDatabase getDb(){

return db;

}

/** Check if the underlying SQLiteDatabase is open

*

* @return whether the DB is open or not

*/

public boolean isOpen(){

return (db!=null&&db.isOpen());

}

/** Lowers the DB counter by 1 for any {@link DatabaseManager}s referencing the same DB on disk

*  
If the new counter is 0, then the database will be closed.

*  
This needs to be called before application exit.


If the counter is 0, then the underlying SQLiteDatabase is null until another DatabaseManager

is instantiated or you call {@link #open()}

*

* @return true if the underlying {@link android.database.sqlite.SQLiteDatabase} is closed (counter is 0),

and false otherwise (counter > 0)

*/

public boolean close(){

sqLiteOpenHelper.removeConnection();

if (sqLiteOpenHelper.getCounter()==0){

synchronized (lockObject){

if (db.inTransaction())db.endTransaction();

if (db.isOpen())db.close();

db = null;

}

return true;

}

return false;

}

/** Increments the internal db counter by one and opens the db if needed

*

*/

public void open(){

sqLiteOpenHelper.addConnection();

if (db==null||!db.isOpen()){

synchronized (lockObject){

db = sqLiteOpenHelper.getWritableDatabase();

}

}

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是创建SQLite数据库的步骤: 1. 打开Android Studio,创建一个新的Empty Activity项目。 2. 打开app/build.gradle文件,并在dependencies中添加以下依赖项: ``` implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:design:28.0.0' implementation 'android.arch.persistence.room:runtime:1.1.1' annotationProcessor 'android.arch.persistence.room:compiler:1.1.1' ``` 3. 创建一个Course类,用于表示课程信息,包括课程名称、教师姓名、上课地点和上课时间等。 ```java @Entity public class Course { @PrimaryKey(autoGenerate = true) private int id; private String name; private String teacher; private String location; private String time; // getters and setters } ``` 4. 创建一个CourseDao接口,用于定义访问Course表的方法,如插入、删除、更新和查询等。 ```java @Dao public interface CourseDao { @Query("SELECT * FROM course") List<Course> getAll(); @Query("SELECT * FROM course WHERE id = :id") Course getById(int id); @Insert void insert(Course course); @Update void update(Course course); @Delete void delete(Course course); } ``` 5. 创建一个CourseDatabase类,用于定义数据库和表的结构,以及提供获取CourseDao实例的方法。 ```java @Database(entities = {Course.class}, version = 1) public abstract class CourseDatabase extends RoomDatabase { public abstract CourseDao courseDao(); private static volatile CourseDatabase INSTANCE; public static CourseDatabase getDatabase(final Context context) { if (INSTANCE == null) { synchronized (CourseDatabase.class) { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(), CourseDatabase.class, "course_database") .build(); } } } return INSTANCE; } } ``` 6. 在MainActivity类中,使用CourseDatabase类创建数据库实例,并使用CourseDao类进行数据库操作。 ```java public class MainActivity extends AppCompatActivity { private CourseDao courseDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); courseDao = CourseDatabase.getDatabase(this).courseDao(); // 插入课程信息 Course course1 = new Course(); course1.setName("Android开发"); course1.setTeacher("张三"); course1.setLocation("教学楼101"); course1.setTime("周一 9:00-11:00"); courseDao.insert(course1); // 查询所有课程信息 List<Course> courses = courseDao.getAll(); // 更新课程信息 Course course2 = courseDao.getById(1); course2.setName("Java编程"); courseDao.update(course2); // 删除课程信息 Course course3 = courseDao.getById(1); courseDao.delete(course3); } } ``` 这样,一个简单的SQLite数据库就创建完成了,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值