由于项目需要一个下载管理模块,最近一直研究文件下载的实现,经过不断的努力终于搞定了!文件下载管理中我们要对文件的信息进行保存,包括文件的大小,文件名,类型等。这时我们就要用到数据库了,android系统为我们提供了轻量级的数据库SQLite。对于SQLite的使用我们可以参考mars的相关视频!下面是我在项目中的实例,供大家参考。
首先定义一个DBHelper继承SQLiteOpenHelper:
package com.cloay.down.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * 保存下载文件信息的DBHelper * DBHelper.java * @author cloay * 2011-11-18 */ public class DBHelper extends SQLiteOpenHelper { // download.db-->数据库名 public DBHelper(Context context) { super(context, "download.db", null, 1); } /** * 在download.db数据库下创建一个download_info表存储下载信息 * state 定义四种下载的状态:初始化状态1,正在下载状态2,暂停状态3,下载完成4 */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, url char, " + "file_size integer, compelete_size integer, state integer)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }然后定义一个DBHelperUtil实现一些常见对数据库的操作功能:
package com.cloay.down.utils; import java.util.ArrayList; import java.util.List; import com.cloay.down.db.DBHelper; import com.cloay.down.entity.FileInfo; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * 数据库工具类,实现数据库的查找,删除,更新等功能 * DBHelperUtil.java * @author cloay * 2011-11-18 */ public class DBHelperUtil { private DBHelper dbHelper; public DBHelperUtil(Context context) { dbHelper = new DBHelper(context); } /** * 查看数据库中是否有数据 * @param urlstr * 下载链接 * @return */ public boolean isHasInfors(String urlstr) { SQLiteDatabase database = dbHelper.getReadableDatabase(); String sql = "select count(*) from download_info where url=?"; Cursor cursor = database.rawQuery(sql, new String[] { urlstr }); cursor.moveToFirst(); int count = cursor.getInt(0); cursor.close(); closeDb(); if(count == 0){ return true; } else{ return false; } } /** * 获取数据库中的数据数 */ public int getLoadInfoCount(){ int i = 0; SQLiteDatabase database = dbHelper.getReadableDatabase(); String sql = "select count(*) from download_info"; Cursor cursor = database.rawQuery(sql, new String[] {}); while(cursor.moveToNext()){ i++; } closeDb(); return i; } /** * 查询数据库中所有数据 */ public List<FileInfo> getLoadInfos (){ List<FileInfo> fileInfos = new ArrayList<FileInfo>(); SQLiteDatabase database = dbHelper.getReadableDatabase(); String sql = "select url,file_size,compelete_size,state from download_info"; Cursor cursor = database.rawQuery(sql, new String[] {}); while (cursor.moveToNext()) { FileInfo info = new FileInfo(cursor.getString(0), cursor.getInt(1), cursor.getInt(2), cursor.getInt(3)); fileInfos.add(info); } cursor.close(); closeDb(); return fileInfos; } /** * 保存 下载的具体信息 * @param info * 下载文件信息 */ public void saveInfos(FileInfo info) { SQLiteDatabase database = dbHelper.getWritableDatabase(); String sql = "insert into download_info(file_size, compelete_size, url, state) values (?,?,?,?)"; Object[] bindArgs = {info.getFileSize(), info.getCompleteSize(), info.getUrlstring(), info.getState()}; database.execSQL(sql, bindArgs); } /** * 得到下载具体信息 * @param urlstr * 下载链接url * @return */ public FileInfo getInfo(String urlstr) { FileInfo info = null; SQLiteDatabase database = dbHelper.getReadableDatabase(); String sql = "select file_size,compelete_size,url,state from download_info where url=?"; Cursor cursor = database.rawQuery(sql, new String[] { urlstr }); while (cursor.moveToNext()) { info = new FileInfo(cursor.getString(2), cursor.getInt(0), cursor.getInt(1), cursor.getInt(3)); } cursor.close(); closeDb(); return info; } /** * 更新数据库中的下载信息 */ public void updataInfos(int compeleteSize, int state, String urlstr) { SQLiteDatabase database = dbHelper.getReadableDatabase(); String sql = "update download_info set compelete_size=?,state=? where url=?"; Object[] bindArgs = { compeleteSize, state, urlstr }; database.execSQL(sql, bindArgs); } /** * 关闭数据库 */ public void closeDb() { dbHelper.close(); } /** * 下载完成后删除数据库中的数据 */ public void delete(String url) { SQLiteDatabase database = dbHelper.getReadableDatabase(); database.delete("download_info", "url=?", new String[] { url }); } }
有问题请留言,大家一起交流学习!
说明:转载请注明出处!