当前位置:网站首页>Android本地数据库基础操作|多线程操作数据库|数据库的增删改查|批量插入数据库|线程池基础使用|玉念聿辉
Android本地数据库基础操作|多线程操作数据库|数据库的增删改查|批量插入数据库|线程池基础使用|玉念聿辉
2022-04-23 00:57:00 【玉念聿辉】
文章素材
本文素材来源于作者(玉念聿辉)的愚蠢操作,最近在一个项目有使用到本地数据库,一段猛如虎的操作下来后发现没法进行多线程操作,大致是报一个数据库被占用的错(具体错误忘记截图了),亲自跑度娘一趟发现使用单例模式可以解决,又一次猛如虎的操作下来发现数据插入太慢等等,因此有了这篇笔记。
数据库
我们都知道Android虽然给我们提供了一个SQLiteOpenHelper,但粗心的朋友很可能跟我一样,稍在一些细节上不注意就会引发大患。
1、认识一下SQLiteOpenHelper
public class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
//在这里创建数据库
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
//在这里可以做升级等
}
}
2、创建单例模式SQLiteOpenHelper
public class DatabaseHelper extends SQLiteOpenHelper {
final String TAG = DatabaseHelper.class.getSimpleName();
private static DatabaseHelper mInstance;
public DatabaseHelper(Context context) {
super(context, "database", null, 1);// 创建数据库名
}
//创建DatabaseHelper
public synchronized static DatabaseHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new DatabaseHelper(context);
}
return mInstance;
}
//关闭DatabaseHelper
public synchronized static void destoryInstance() {
if (mInstance != null) {
mInstance.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table table_name(id Integer primary key autoincrement, note text,changeTime text)");// 创建表
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
3、单例模式下的增删改查
/**
*不进行批量插入的插入方法(不开启事务)
*/
public synchronized void insertData(){
ContentValues values=new ContentValues();
values.put("note ", "备注");
values.put("changeTime ", new date());
getWritableDatabase().insert("table_name", null, values);
}
/**
*批量插入(开启事务)
*/
public synchronized void insertData(List<ContentValues> cvList) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
for (ContentValues values : cvList) {
db.insert("init_table", null, values);
}
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
} catch (Exception e) {
e.printStackTrace();
} finally {
// 结束事务
db.endTransaction();
db.close();
Log.i(TAG, "插入数据库完成");
}
}
/**
* 查询数据库中table_name表数据的总条数.
* * @return
*/
public synchronized int getAllCaseNum() {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
String sql = "select count(*) from table_name";
Cursor cursor = null;
long count = 0;
try {
cursor = db.rawQuery(sql, null);
cursor.moveToFirst();
count = cursor.getLong(0);
Log.i(TAG, "init_table表总数据条数: " + count);
db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
// 结束事务
db.endTransaction();
db.close();
}
return (int) count;
}
注:
* 多线程管理开启单例模式即可(synchronized);
* 数据库的增删改查尽量开启事务来执行,执行完要记得关闭;
* Cursor使用结束记得关闭;
参见文章:https://www.cnblogs.com/zhujiabin/p/4240769.html
线程池
通过将SQLiteOpenHelper设置单例模式后,我们还是需要线程池来调用才能确保速度上的提升了,这里就简单贴一下需要注意的地方,如线程池大小、空闲时间等(也就是ThreadPoolExecutor的参数设置)。

1、示列
private static final BlockingQueue<Runnable> POOL_QUEUE_TASK = new SynchronousQueue<Runnable>();
private static final ThreadFactory TASK_FACTORY = new ThreadFactory() {
private final AtomicInteger COUNT = new AtomicInteger(1);
public Thread newThread(Runnable runnable) {
int count = COUNT.getAndIncrement();
return new Thread(runnable, "Func #" + count);
}
};
/**
* 线程池
*/
public static final ExecutorService FUNC_TASK = new ThreadPoolExecutor(8,
Integer.MAX_VALUE, 3L, TimeUnit.SECONDS, POOL_QUEUE_TASK,
TASK_FACTORY);
2、调用
FUNC_TASK.execute(new Runnable() {
@Override
public void run() {
//例如在这里进行调用插入数据的方法
}
});
总结
开发速度固然重要,但实际过程中我们往往会忽略一些简单的细节处理,希望每一次bug都是我们成长的见证,文章仅作为笔记,如有错误欢迎大家指点。
最后推荐一款学习硬件开发的IDE,对于我这种菜鸟C|C++的人来说,Arduino确实是一个不错的选择。
我的第一个demo:https://blog.csdn.net/qq_35350654/article/details/94876016
Arduino学习资料:https://www.arduino.cn/thread-1066-1-1.html
版权声明
本文为[玉念聿辉]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_35350654/article/details/96440345
边栏推荐
- 软件测试神仙文档,连阿里面试官都说太详细了,搞懂这些直接是P7级
- 信息系统项目管理-立项管理
- L2-012 judgment on heap (25 points) (string bug to be solved)
- be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
- What is tooljet and how about it—— Evaluation of low code development platform
- DCB“一哥”先瑞达,靠什么拉升价值曲线?
- C#/. Net uses questpdf operation to generate PDF faster and more efficient!
- Acrel-5000型建筑能耗监测系统在西咸空港花园项目的研究与应用
- Why should I object to DBA's participation in business (issuing reports / changing data)
- Information system project management - project initiation management
猜你喜欢

2.56 - try running show with different sample values_ Bytes code.
![[ACTF2020 新生赛]Include](/img/0c/4c06112383c0b225c987a499b622a9.png)
[ACTF2020 新生赛]Include

This new feature of C 11, I would like to call it the strongest!

Amazon Aurora's ability to read and write: shardingsphere proxy
![[HCTF 2018]admin](/img/08/8111f2f87796dda8dd6b0dcf72c42b.png)
[HCTF 2018]admin

智能照明控制系统在医院的设计与应用

Let's talk about ruby

DCB“一哥”先瑞达,靠什么拉升价值曲线?

德勤2022技术趋势:IT自我颠覆、技术跨界融合创新

The common public page cannot be imported into the templates subset directory of thymeleaf, otherwise an error will be reported: template parsing error, error reason: the reference path of the public
随机推荐
Xamarin effect Chapter 22 recording effect
ethtool查看网卡统计信息的流程
软考不知道该如何选科?这篇文章解答你的疑惑
Kotlin - inherit open
安全用电管理平台在靖边博物馆安全用电管理系统的应用
Secret of 66% performance surge: AMD 25000 yuan 768mb 3D cache Xiaolong opened the cover for the first time
Elk setup (IV): monitor MySQL slow query and error log
Can you really cross with a k-fold? Thoughts on k-fold intersection
On the soft test, these common questions must be understood
【服务器数据恢复】服务器硬盘进水后服务器崩溃的数据恢复案例
Soft exam do not know how to choose subjects? This article answers your doubts
The more "intelligent" the machine is, the easier it is for data taggers to be eliminated? Manfu Technology
2.58 - write the program is little endian, which returns 1 when compiled and run on the small end method machine and 0 when compiled and run on the large end method machine. This program should be abl
Daily practice (47): find different
The common public page cannot be imported into the templates subset directory of thymeleaf, otherwise an error will be reported: template parsing error, error reason: the reference path of the public
Terminal mobile JD Mall
The thymeleaf template < img th: SRC = "${map. User. Headerurl}" used in idea reports an error cannot resolve 'user‘
2.56 - try running show with different sample values_ Bytes code.
Enterprises choose to privatize the deployment of IM instant messaging software to fully protect information security!
DCB "first brother" xianruida, what does it rely on to raise the value curve?