欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android的SQLite数据库示例项目(线程安全版)_安卓SQLite数据库实例

程序员文章站 2022-03-01 16:25:14
...

这是一个教程以线程安全的方式访问在Android的SQLite数据库

介绍

本文旨在说明最佳实践以线程安全的方式访问一个Android的数据库。这里使用的WorxForUs框架有助于做许多共同的任务:

序列化访问数据库,执行每个表的升级,以及更先进的同步功能,以保持脱机应用程序数据库与远程数据库的同步。

这也提供了一种替代方法来编写一个复杂的ContentProvider为您的应用程序。

背景

我写了这个库,因为我我的应用程序是基于Android的例子,虽然他们的工作的大部分时间,我经常得到错误:

android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
java.lang.IllegalStateException: database not open
android.database.sqlite.SQLiteException: database is locked
Failed to setLocale() when constructing, closing the database


我是没有考虑到的诀窍是让序列化的访问我的数据库。这工作得很好单线程设备,但多核心设备通常哽咽。

为了防止这种情况我用一个单访问数据库使用信号来提供数据库的锁,以便只有一个线程可以同时访问它。

使用代码

首先,下载上面显示的WorxForUs框架和示例项目。

导入项目到Eclipse或Android工作室。

确保样本项目是通过添加引用来从属性/ Android上worxforus_library项目链接到库项目。

主应用程序

请参见下面有关如何主程序使用的数据库访问的样本。要特别小心,每一个访问表对象的说明

AcquireConnection(..)和releaseConnection(..)方法之间。这些方法是什么序列化访问它。让您的访问简短而亲切。

//create the database table - note a TablePool could be used here
NuggetTable nuggetTable = new NuggetTable(this);

//lock the database access for use by this thread
TableManager.acquireConnection(this, NuggetTable.DATABASE_NAME, table);
//insert your table action here

Nugget nugget = new Nugget();
nugget.setType((int)Math.round(Math.random()*3));
//add item to the table - here I am ignoring the returned results which includes the 
//insert id so it can be retrieved
table.insert(nugget);
//get the list of all objects in the table
ArrayListlist = table.getAllEntries();

//release the database access so other threads can no access it.
TableManager.releaseConnection(nuggetTable);
模型
来看看我们存储的数据模型。在这种情况下它是一个矿石块的简单的信息。



package com.example.worxforusdbsample;

public class Nugget {
    String type="";
    int id =0;
  
    public static final String IRON = "Iron";
    public static final String GOLD = "Gold";
    public static final String DIAMOND = "Diamond";

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public void setType(int type) {
        if (type == 1)
            setType(GOLD);
        else if (type == 2)
            setType(DIAMOND);
        else //set remaining to Iron
            setType(IRON);
    }
  
    public int getId() {
        return id;
    }
  
    public void setId(int id) {
        this.id = id;
    }
  
    public String getDescription() {
        return type+" nugget";
    }
}
表 - 如何在基础数据库表的定义

该NuggetTable.java文件显示我们是如何创造我们的链接到数据库。这个类从TableInterface它提供了一个接口,

使我们能够做所有的事情整齐到数据库,如提供访问,创建和更新表的标准方式延伸。这里的主要问题是,由于我们使用的是一个接口,

我们可以访问一切与一个TableManager。这样做的好处是,TableManager已经知道如何判断是否已经创建的数据库或否,它需要被更新,以及如何执行更新。

你会在这段代码中,首先映入眼帘的是一堆静态最终字符串和整数定义表中的数据字段和SQL代码在数据库中创建表。

重载方法被定义为数据库帮手的开放和关闭这是Android的SQLite数据库项目中的标准SQLiteOpenHelper的。

除了insert方法,包括我刚刚使用sqlite的替代方法,如果你不希望(或需要)来检查数据库是否行具有相同的主键已经存在的insertOrUpdate方法。

虽然这有轻微的性能损失,因为替换实际上做了删除和幕后插入。

值得一提的另一个项目是加入了注释的ONUPGRADE辅助函数,显示了如何更改您的数据库和系统数据库表被访问的下一次自动处理它们。只是每次你改变它,并添加一个数据库命令作为的public static final String要在检测到变化,下一次运行递增TABLE_VERSION场在你的代码。

以下代码请在源码中下载:

WorxForUs_Library.zip(第三方包)下载链接: http://dwtedx.com/download.html?bdkey=s/1i3eKsPv 密码: sgi9

线程安全的数据库SQLite源码 下载链接: http://dwtedx.com/download.html?bdkey=s/1i3utw8X 密码: omtd