Android SQLite in download management

Source: Internet
Author: User

Because the project requires a download management module, we have been studying the implementation of File Download recently, and have finally finished it through continuous efforts! In File Download management, we need to save the file information, including the file size, file name, and type. Now we need to use the database. The android system provides us with a lightweight database SQLite. For how to use SQLite, refer to the related video of mars! The following is an example of my project for your reference.

First, define a DBHelper that inherits SQLiteOpenHelper:

Package com. cloay. down. db; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper;/*** DBHelper * DBHelper that saves the downloaded file information. java * @ author cloay * 2011-11-18 */public class DBHelper extends SQLiteOpenHelper {// download. db --> database name public DBHelper (Context context) {super (context, "download. db ", null, 1);}/*** in download. create a download_info table under the db database to store the download information * state defines four download statuses: initialization status 1, download status 2, pause status 3, download completed 4 */@ Overridepublic void onCreate (SQLiteDatabase db) mongodb.exe cSQL ("create table download_info (_ id integer primary key autoincrement, url char," + "file_size integer, compelete_size integer, state integer) ") ;}@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){}}

Then define a DBHelperUtil to implement some common database operation functions:

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;/*** database tool class for searching, deleting, and updating databases * DBHelperUtil. java * @ author cloay * 2011-11-18 */public class DBHelperUtil {private DBHelper dbHelper; public DBHelperUtil (Context context) {dbHelper = new DBHelper (context );} /*** check whether data exists in the database * @ param urlstr * download link * @ 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 ;}} /*** get the data count in the database */public int getLoadInfoCount () {int I = 0; SQLiteDatabase database = dbHelper. getReadableDatabase (); String SQL = "select count (*) from download_info"; Cursor cursor = database. raw Query (SQL, new String [] {}); while (cursor. moveToNext () {I ++;} closeDb (); return I;}/*** query all data in the database */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 (cu Rsor. 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;}/*** Save the download details * @ param info * download file information */public void saveInfos (FileInfo info) {SQLiteDatabase database = dbHelper. getWritableDatabase (); String SQL = "insert into download_info (file_size, compelete_siz E, url, state) values (?,?,?,?) "; Object [] bindArgs = {info. getFileSize (), info. getCompleteSize (), info. getUrlstring (), info. getState ()}; database.exe cSQL (SQL, bindArgs );} /*** get download details * @ param urlstr * download 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;}/*** update the download information in the database */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.exe cSQL (SQL, bindArgs);}/*** close database */public void closeDb () {dbHelper. close ();}/*** delete database data after download */public void delete (String url) {SQLiteDatabase database = dbHelper. getReadableDatabase (); database. delete ("download_info", "url =? ", New String [] {url });}}

Through database operations, we can save the current information about the file and manage the file download.

Please leave a message if you have any questions!

Note: Please indicate the source for reprinting!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.