- 浏览: 23181 次
文章分类
最新评论
-
chaozhidan:
楼主有demo么,为什么我的显示不出来呢,646869341@ ...
自定义GridView以解决ScrollView嵌套Android自身GridView出现的疑难问题 -
吝啬你的温柔:
没用用过吗?
自定义GridView以解决ScrollView嵌套Android自身GridView出现的疑难问题
在Android项目开发中会经常使用到其内嵌的SQLite数据库,但是疲于Android本身的SQLiteOpenHelper的繁琐(也许个人从事过Web开发,惯用MySql的偏见),经过统一思想的 方式封装了该sqlite-helper.jar包,对于从Java Web方向转过来的人也许算是一种帮助吧!
1.新建实体类
2.该实体类的Dao接口
3.该实体类的Dao接口实现类
4.该实体类的Service类
当然,对于习惯写SQL语句的人来说这种方式很容易理解,jar中基本上已经封装好了常用的增删改查,特殊处理的地方也就是如上书写SQL语句并传递参数并调用响应的基本方法即可。
5.真正使用
做完上述基本工作以后接下来就是如何去使用,使用其实很简单,首先如果是新建的数据库就需要SQL语句:
接着就是其他的一些操作只需获取响应实体类的Service对象:
这里需要说明的是实体类的属性名必须与数据库表中的字段名一样(虽然不区分大小写),实体名也与对应的数据表名一样(虽然不区分大小写),否则会报异常,这样的好处是方便明白意义。上述只是创建了一个数据表,该表的SQL语句如下:
1.新建实体类
package com.whl.helper.entity; import com.fasterxml.jackson.annotation.JsonIgnore; public class DownloadInfo { @JsonIgnore(value=true) private int id; private int threadId;// 下载器id private int startPos;// 开始点 private int endPos;// 结束点 private int completeSize;// 完成度 private String downloadUrl;// 下载器网络标识 private int finish;//下载完成(0:未完成,1:已完成) public DownloadInfo(){} public DownloadInfo(int threadId, int startPos, int endPos,int completeSize, String downloadUrl) { super(); this.threadId = threadId; this.startPos = startPos; this.endPos = endPos; this.completeSize = completeSize; this.downloadUrl = downloadUrl; } public int getId(){ return id; } public void setId(int id){ this.id = id; } public int getThreadId() { return threadId; } public void setThreadId(int threadId) { this.threadId = threadId; } public int getStartPos() { return startPos; } public void setStartPos(int startPos) { this.startPos = startPos; } public int getEndPos() { return endPos; } public void setEndPos(int endPos) { this.endPos = endPos; } public int getCompleteSize() { return completeSize; } public void setCompleteSize(int completeSize) { this.completeSize = completeSize; } public String getDownloadUrl() { return downloadUrl; } public void setDownloadUrl(String downloadUrl) { this.downloadUrl = downloadUrl; } public int getFinish() { return finish; } public void setFinish(int finish) { this.finish = finish; } @Override public String toString() { return "DownloadInfo [id=" + id + ", threadId=" + threadId + ", startPos=" + startPos + ", endPos=" + endPos + ", completeSize=" + completeSize + ", downloadUrl=" + downloadUrl + ", finish=" + finish + "]"; } }
2.该实体类的Dao接口
package com.whl.helper.database.dao; import java.util.List; import com.whl.dao.common.BaseDao; import com.whl.dao.common.DaoException; import com.whl.helper.entity.DownloadInfo; public interface DownloadInfoDao extends BaseDao<DownloadInfo> { public Boolean isHasDownloadInfosWithDownloadUrl(String downloadUrl) throws DaoException; public List<DownloadInfo> findDownloadInfoListWithDownloadUrl(String downloadUrl) throws DaoException; public int deleteDownloadInfoWithDownloadUrl(String downloadUrl) throws DaoException; public int update(int completeSize,int threadId,String downloadUrl) throws DaoException; public DownloadInfo findDownloadInfoWithThreadIdAndDownloadUrl(int threadId, String downloadUrl) throws DaoException; public long findDownloadInfoSumCompleteSizeWithDownloadUrl(String downloadUrl) throws DaoException; }
3.该实体类的Dao接口实现类
package com.whl.helper.database.dao.impl; import java.util.List; import android.content.Context; import android.database.Cursor; import com.whl.dao.common.DaoException; import com.whl.dao.impl.CreateBaseDaoImpl; import com.whl.helper.database.dao.DownloadInfoDao; import com.whl.helper.entity.DownloadInfo; import com.whl.utils.RowMapper; public class DownloadInfoDaoImpl extends CreateBaseDaoImpl<DownloadInfo> implements DownloadInfoDao { public DownloadInfoDaoImpl(Context context){ super.prepare(context, new RowMapper<DownloadInfo>() { @Override public DownloadInfo onRowMapper(Cursor cursor) { DownloadInfo downloadInfo = new DownloadInfo(); downloadInfo.setId(cursor.getInt(cursor.getColumnIndex("id"))); downloadInfo.setCompleteSize(cursor.getInt(cursor.getColumnIndex("completeSize"))); downloadInfo.setDownloadUrl(cursor.getString(cursor.getColumnIndex("downloadUrl"))); downloadInfo.setEndPos(cursor.getInt(cursor.getColumnIndex("endPos"))); downloadInfo.setStartPos(cursor.getInt(cursor.getColumnIndex("startPos"))); downloadInfo.setThreadId(cursor.getInt(cursor.getColumnIndex("threadId"))); downloadInfo.setFinish(cursor.getInt(cursor.getColumnIndex("finish"))); return downloadInfo; } }); } @Override public Boolean isHasDownloadInfosWithDownloadUrl(String downloadUrl) throws DaoException { String sql = "SELECT COUNT(*) AS size FROM downloadInfo WHERE downloadUrl = ?"; String[] values = {downloadUrl}; return super.countSqlResult(sql, values)==0?true:false; } @Override public List<DownloadInfo> findDownloadInfoListWithDownloadUrl(String downloadUrl) throws DaoException { String sql = "SELECT * FROM downloadInfo WHERE downloadUrl = ?"; String[] values = {downloadUrl}; //List<DownloadInfo> downloadInfoList = super.find(sql, values); return super.find(sql, values); } @Override public int deleteDownloadInfoWithDownloadUrl(String downloadUrl)throws DaoException { String sql = "DELETE FROM downloadInfo WHERE downloadUrl = ?"; String[] values = {downloadUrl}; return super.deleteUnique(sql, values); } @Override public int update(int completeSize,int threadId,String downloadUrl) throws DaoException { String sql = "UPDATE downloadInfo SET completeSize = ? WHERE threadId=? AND downloadUrl = ?"; String[] values = {String.valueOf(completeSize),String.valueOf(threadId),downloadUrl}; return super.batchExecute(sql, values); } @Override public DownloadInfo findDownloadInfoWithThreadIdAndDownloadUrl(int threadId, String downloadUrl) throws DaoException{ String sql = "SELECT * FROM downloadInfo WHERE threadId = ? AND downloadUrl = ?"; String[] values = {String.valueOf(threadId),downloadUrl}; return super.findUniqueResult(sql, values); } @Override public long findDownloadInfoSumCompleteSizeWithDownloadUrl(String downloadUrl) throws DaoException { long sumCompletedSize = 0; List<DownloadInfo> downloadInfoList = findDownloadInfoListWithDownloadUrl(downloadUrl); for (DownloadInfo downloadInfo : downloadInfoList) { sumCompletedSize+=downloadInfo.getCompleteSize(); } return sumCompletedSize; } }
4.该实体类的Service类
package com.whl.helper.database.service; import java.util.List; import android.content.Context; import com.whl.dao.common.DaoException; import com.whl.helper.database.dao.DownloadInfoDao; import com.whl.helper.database.dao.impl.DownloadInfoDaoImpl; import com.whl.helper.entity.DownloadInfo; import com.whl.service.common.BaseService; public class DownloadInfoService extends BaseService<DownloadInfo> { private DownloadInfoDao downloadInfoDao; private static DownloadInfoService downloadInfoServiceInstance; private DownloadInfoService(Context context){ downloadInfoDao = new DownloadInfoDaoImpl(context); super.prepareBaseDao(downloadInfoDao); } public static DownloadInfoService getInstance(Context context){ if(downloadInfoServiceInstance==null){ downloadInfoServiceInstance = new DownloadInfoService(context); } return downloadInfoServiceInstance; } public Boolean isHasDownloadInfosWithDownloadUrl(String downloadUrl){ try { return downloadInfoDao.isHasDownloadInfosWithDownloadUrl(downloadUrl); } catch (DaoException e) { e.printStackTrace(); return false; } } public List<DownloadInfo> findDownloadInfoListWithDownloadUrl(String downloadUrl){ try { return downloadInfoDao.findDownloadInfoListWithDownloadUrl(downloadUrl); } catch (DaoException e) { e.printStackTrace(); return null; } } public int deleteDownloadInfoWithDownloadUrl(String downloadUrl){ try { return downloadInfoDao.deleteDownloadInfoWithDownloadUrl(downloadUrl); } catch (DaoException e) { e.printStackTrace(); return 0; } } public int update(int completeSize,int threadId,String downloadUrl){ try { return downloadInfoDao.update(completeSize,threadId, downloadUrl); } catch (DaoException e) { e.printStackTrace(); return 0; } } public DownloadInfo findDownloadInfoWithThreadIdAndDownloadUrl(int threadId, String downloadUrl){ try { return downloadInfoDao.findDownloadInfoWithThreadIdAndDownloadUrl(threadId, downloadUrl); } catch (DaoException e) { e.printStackTrace(); return null; } } public long findDownloadInfoSumCompleteSizeWithDownloadUrl(String downloadUrl){ try { return downloadInfoDao.findDownloadInfoSumCompleteSizeWithDownloadUrl(downloadUrl); } catch (DaoException e) { e.printStackTrace(); return 0; } } }
当然,对于习惯写SQL语句的人来说这种方式很容易理解,jar中基本上已经封装好了常用的增删改查,特殊处理的地方也就是如上书写SQL语句并传递参数并调用响应的基本方法即可。
5.真正使用
做完上述基本工作以后接下来就是如何去使用,使用其实很简单,首先如果是新建的数据库就需要SQL语句:
/** * 初始化数据库 */ public void initDatabase(String[] sqlArray){ CreateDatabaseHelper createDatabaseHelper = CreateDatabaseHelper.getInstanceDatabaseHelper(getApplicationContext()); SQLiteDatabase database = createDatabaseHelper.getWritableDatabase(); database.beginTransaction(); for (String sql : sqlArray) { database.execSQL(sql); } database.setTransactionSuccessful(); database.endTransaction(); database.close(); }
接着就是其他的一些操作只需获取响应实体类的Service对象:
public void testSqliteStatement(){ DownloadInfoService downloadInfoService = DownloadInfoService.getInstance(this); try { long startTime = System.currentTimeMillis(); DownloadInfo downloadInfo = downloadInfoService.get("2"); downloadInfoService.removeUnique(downloadInfo); long finishTime = System.currentTimeMillis(); Log.d(TAG, "testSqliteStatement:"+(finishTime-startTime)); } catch (DaoException e) { e.printStackTrace(); } /*List<DownloadInfo> downloadInfoList = new ArrayList<DownloadInfo>(); for(int i=0;i<2;i++){ DownloadInfo downloadInfo = new DownloadInfo(); downloadInfo.setCompleteSize(1000+i); downloadInfo.setDownloadUrl("www"+i); downloadInfo.setEndPos(2000+i); downloadInfo.setFinish(0+i); downloadInfo.setStartPos(0+i); downloadInfo.setThreadId(11+i); downloadInfoList.add(downloadInfo); } try { long startTime = System.currentTimeMillis(); for (DownloadInfo downloadInfo : downloadInfoList) { downloadInfoService.insert(downloadInfo); } long finishTime = System.currentTimeMillis(); Log.d(TAG, "testSqliteStatement:"+(finishTime-startTime)); } catch (DaoException e) { e.printStackTrace(); }*/ }
这里需要说明的是实体类的属性名必须与数据库表中的字段名一样(虽然不区分大小写),实体名也与对应的数据表名一样(虽然不区分大小写),否则会报异常,这样的好处是方便明白意义。上述只是创建了一个数据表,该表的SQL语句如下:
CREATE TABLE IF NOT EXISTS downloadInfo(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, threadId INTEGER, startPos INTEGER, endPos INTEGER, completeSize INTEGER,downloadUrl VARCHAR,finish INTEGER)
- sqlite-helper.jar (41.8 KB)
- 下载次数: 180
- jackson-annotations-2.2.2.jar (32.7 KB)
- 下载次数: 92
- jackson-core-2.2.2.jar (187.2 KB)
- 下载次数: 91
- jackson-databind-2.2.2.jar (845.8 KB)
- 下载次数: 86
相关推荐
官方版本,亲测可用
赠送jar包:sqlite-jdbc-3.34.0.jar; 赠送原API文档:sqlite-jdbc-3.34.0-javadoc.jar; 赠送源代码:sqlite-jdbc-3.34.0-sources.jar; 赠送Maven依赖信息文件:sqlite-jdbc-3.34.0.pom; 包含翻译后的API文档:...
sqlite-jdbc-3.36.0.3.jar 最新吧2021 8月底更新
sqlite-jdbc-3.20.1.jar 连接Java和sqlite的最新驱动!
SQLite数据库是文档型数据库,其具备体积小移动方便等特点;以下jar包:sqlite-jdbc-3.30.1.jar文件为SQLite数据库对应的数据库驱动jar包;
sqlite-jdbc-3.34.0.jar,支持M1芯片了,Android Studio可以正常链接手机了
而`sqlite-jdbc-3.8.7.jar`是SQLite JDBC驱动的特定版本,该版本为3.8.7。 在Java中,JDBC(Java Database Connectivity)是一种标准的API,允许Java应用程序通过Java代码与各种类型的数据库进行通信。SQLite JDBC...
本主题主要围绕“Sqlite-jdbc-3.7.2.jar”和“sqlite-jdbc-3.20.1.jar”两个Java JDBC驱动进行讨论,它们是Java连接SQLite数据库的关键组件。 Sqlite-jdbc驱动是Java开发者用来与SQLite数据库进行交互的桥梁。JDBC...
sqlite3的驱动jar包,sqlite-jdbc-3.21.0.jar,支持sqlite3数据库
在本场景中,我们关注的是如何使用 `sqlite-jdbc-3.8.11.1.jar` 这个特定版本的驱动来实现 Java 与 SQLite 的交互。 首先,要使用 SQLite JDBC,你需要将 `sqlite-jdbc-3.8.11.1.jar` 添加到你的项目类路径中。如果...
首先,我们需要了解如何在Java项目中使用sqlite-jdbc-3.31.1.jar。这个JAR文件包含了SQLite JDBC驱动的所有必要类和资源,使得Java程序能够通过JDBC API与SQLite数据库通信。为了使用这个驱动,你需要将sqlite-jdbc-...
sqlite-jdbc-3.32.3-sources.jar;嵌入式数据库sqlite的源码,喜欢的可以看看。
"sqlite-jdbc-3.32.3.2.jar"是这个驱动程序的特定版本,它包含了用于连接SQLite数据库的必要组件。 这个版本的sqlite-jdbc驱动特别强调对信创(创新信息技术)环境的支持,这通常指的是中国自主研发的CPU架构和操作...
赠送jar包:sqlite-jdbc-3.34.0.jar; 赠送原API文档:sqlite-jdbc-3.34.0-javadoc.jar; 赠送源代码:sqlite-jdbc-3.34.0-sources.jar; 赠送Maven依赖信息文件:sqlite-jdbc-3.34.0.pom; 包含翻译后的API文档:...
SQLite JDBC驱动程序是Java开发人员用来连接SQLite数据库的桥梁,标题中的"sqlite-jdbc-3.32.3.2.jar"就是这个特定版本的驱动程序。SQLite是一款轻量级的关系型数据库管理系统,广泛应用于嵌入式系统和移动应用中,...
sqlite-jdbc-3.27.2.1.jar包
可以在mips64el/loongarch64系统上运行。 https://quantum6.blog.csdn.net/article/details/119570408
1. **sqlite-jdbc-3.27.2.1.jar**:这是SQLite JDBC驱动程序,使得Java应用(如GeoServer)能够与SQLite数据库进行交互。这个版本的驱动支持SQLite 3.27.2.1版本的数据库。 2. **gwc-sqlite-1.15-SNAPSHOT.jar**:...
官网下载地址:https://bitbucket.org/xerial/sqlite-jdbc/downloads/ 只是经常网络不好,连不上!不想费劲的,可以从这里下! 本来想设0分,资源分最少只能选2分,没办法啦!