How to import an external database file larger than 1 MB for Android

Source: Internet
Author: User
How to import an external database file larger than 1 MB for Android? pm I want to comment (0) font size: T | T

Android imports an existing external database file larger than 1 MB.

AD:


    1. If the database file is larger than 1 MB, use the filesplit tool to cut. First download the software tool 2. first put the existing database under the assets folder. If this file does not exist, create this folder in the android project. The Code is as follows: Import java. Io. file;
    Import java. Io. fileoutputstream;
    Import java. Io. ioexception;
    Import java. Io. inputstream;
    Import java. Io. outputstream;

    Import Android. content. context;
    Import Android. database. cursor;
    Import Android. database. sqlexception;
    Import Android. database. SQLite. sqlitedatabase;
    Import Android. database. SQLite. sqlitedatabase. cursorfactory;
    Import Android. database. SQLite. sqliteexception;
    Import Android. database. SQLite. sqliteopenhelper;

    * The database files under assets will be copied directly to db_path, but the database file size is limited to less than 1 MB.
    * If there are large files larger than 1 MB, you must first split them into N small files, and then replace copybigdatabase () with copydatabase ()
    */
    Public class dbmanager extends sqliteopenhelper {
    // Version of the user database file
    Private Static final int db_version = 1;
    // The storage path of the database file is the default location. com. Rys. lb is your package name.
    Private Static string db_path = "/data/COM. Rys. lb/databases /";

    // If You Want To store database files on the SD card
    // Private Static string db_path = Android. OS. environment. getexternalstoragedirectory (). getabsolutepath ()
    // + "/Arthur urcn/drivertest/packfiles /";

    Private Static string db_name = "data. DB ";
    Private Static string assets_name = "data. DB ";

    Private sqlitedatabase mydatabase;
    Private Final context mycontext;

    /**
    * If the database file is large, use filesplit to split it into small files smaller than 1 MB.
    * In this example, Data. db.100 data. db.101 data. db.102 ....
    */
    // Suffix of the first file name
    Private Static final int assets_suffix_begin = 100;
    // Suffix of the last file name
    Private Static final int assets_suffix_end = 110;

    /**
    * This constructor must be included in the subclass of sqliteopenhelper.
    * @ Param context object
    * @ Param name: Database Name
    * @ Param factory is generally null.
    * @ Param version: version of the current database. The value must be an integer and increase progressively.
    */
    Public dbmanager (context, string name, cursorfactory factory, int version ){
    // The constructor in the parent class must be called through Super
    Super (context, name, null, version );
    This. mycontext = context;
    }

    Public dbmanager (context, string name, int version ){
    This (context, name, null, version );
    }

    Public dbmanager (context, string name ){
    This (context, name, db_version );
    }

    Public dbmanager (context ){
    This (context, db_path + db_name );
    }

    Public void createdatabase () throws ioexception {
    Boolean dbexist = checkdatabase ();
    If (dbexist ){
    // The database already exists. Do nothing.

    System. Out. println ("database already exists ");

    } Else {
    // Create a database
    Try {
    File dir = new file (db_path );
    If (! Dir. exists ()){
    Dir. mkdirs ();
    }
    File DBF = new file (db_path + db_name );
    If (DBF. exists ()){
    DBF. Delete ();
    }
    Sqlitedatabase. openorcreatedatabase (DBF, null );
    // Copy the DB file in asseets to db_path.
    // Copydatabase ();
    Copybigdatabase ();
    } Catch (ioexception e ){
    Throw new error ("database creation failed ");
    }
    }
    }

    // Check whether the database is valid
    Private Boolean checkdatabase (){
    Sqlitedatabase checkdb = NULL;
    String mypath = db_path + db_name;
    Try {
    Checkdb = sqlitedatabase. opendatabase (mypath, null, sqlitedatabase. open_readonly );
    } Catch (sqliteexception e ){
    // Database does't exist yet.
    }
    If (checkdb! = NULL ){
    Checkdb. Close ();
    System. Out. println ("disabled ");
    }
    Return checkdb! = NULL? True: false;
    }
    Public dbmanager open1 (){
    String mypath = db_path + db_name;
    System. Out. println ("database already ...");
    Mydatabase = sqlitedatabase. opendatabase (mypath, null, sqlitedatabase. open_readonly );
    System. Out. println ("Open Database ");
    Return this;

    }
    /**
    * Copies your database from your local assets-folder to the just created empty database in
    * System folder, from where it can be accessed and handled.
    * This is done by transfering bytestream.
    **/
    Private void copydatabase () throws ioexception {
    // Open your local dB as the input stream
    Inputstream myinput = mycontext. getassets (). Open (assets_name );
    // Path to the just created empty DB
    String outfilename = db_path + db_name;
    // Open the empty dB as the output stream
    Outputstream myoutput = new fileoutputstream (outfilename );
    // Transfer bytes from the inputfile to the outputfile
    Byte [] buffer = new byte [1024];
    Int length;
    While (length = myinput. Read (buffer)> 0 ){
    Myoutput. Write (buffer, 0, length );
    }
    // Close the streams
    Myoutput. Flush ();
    Myoutput. Close ();
    Myinput. Close ();
    }

    // This is used when copying large database files under assets
    Private void copybigdatabase () throws ioexception {
    Inputstream myinput;
    String outfilename = db_path + db_name;
    Outputstream myoutput = new fileoutputstream (outfilename );
    For (INT I = assets_suffix_begin; I <assets_suffix_end + 1; I ++ ){
    Myinput = mycontext. getassets (). Open (assets_name + "." + I );
    Byte [] buffer = new byte [1024];
    Int length;
    While (length = myinput. Read (buffer)> 0 ){
    Myoutput. Write (buffer, 0, length );
    }
    Myoutput. Flush ();
    Myinput. Close ();
    }
    Myoutput. Close ();
    System. Out. println ("database copied ");
    }

    @ Override
    Public synchronized void close (){
    If (mydatabase! = NULL ){
    Mydatabase. Close ();
    System. Out. println ("disabled successfully 1 ");
    }
    Super. Close ();
    System. Out. println ("disabled successfully 2 ");
    }

    /**
    * This function is executed at the first creation,
    * This method is called only when the sqlitedatabase object is obtained for the first time.
    */
    @ Override
    Public void oncreate (sqlitedatabase dB ){
    }

    /**
    * Used when the database table structure changes
    */
    @ Override
    Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){
    }
    Public void open (){
    Sqlitedatabase database = This. openorcreatedatabase ("data. DB", null );
    } Private sqlitedatabase openorcreatedatabase (string, object ){
    // Todo auto-generated method stub
    Return NULL;
    } 3. add permissions to the SD card <uses-Permission Android: Name = "android. Permission. write_external_storage"> </uses-Permission>
    <Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"> </uses-Permission>

    [Edit recommendations]

    1. Are Google ready to expand the Nexus program? Why is Android saved?
    2. Mobile systems are getting better and better: Google brother, pull android
    3. The number of Android malware is rising like a rocket
    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.