Getting Started with Android development (19) database 19.3 pre-create database

Source: Internet
Author: User
Tags sqlite database create database

In the development process, it is sometimes more efficient to create a database beforehand than to create it when the program is running. For example, you want to write a program that shows the coordinates of the places you've been to. In this case, it is much easier to create a database beforehand than to create a database at run time.

Here, you need to use some free tools. Recommended use of SQLite Database Browser, support multi-platform, and free. Download Address: http://sourceforge.net/projects/sqlitebrowser/

The following is an example of creating a Contact table.

In the design phase of the database, the next step is to bind the database and the program together, so that in the runtime, you can use the database.

1. Put the database under the Assets folder.

2. Copy the database below the assets folder to the installation path of the program.

The public class Databasesactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Override  
        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  
      
        Setcontentview (R.layout.main);  
              
        Dbadapter db = new Dbadapter (this);  
            try {String destpath = "/data/data/" + getpackagename () + "/databases";  
            File F = new file (destpath);  
                if (!f.exists ()) {f.mkdirs ();  
                   
             F.createnewfile (); ---Copy the DB from the Assets folder into//the Databases folder---copydb (getbaseco  
            ntext (). Getassets (). Open ("MyDB"), new FileOutputStream (DestPath + "/mydb"));  
        } catch (FileNotFoundException e) {e.printstacktrace ();  
           catch (IOException e) { E.printstacktrace ();  
        }//---Get all contacts---db.open ();  
        Cursor C = db.getallcontacts ();  
            if (C.movetofirst ()) {do {displaycontact (c);  
        while (C.movetonext ());  
    } db.close ();  
        public void Copydb (InputStream inputstream, OutputStream outputstream) throws IOException {  
        ---copy 1K bytes at a time---byte[] buffer = new byte[1024];  
        int length;  
        while (length = inputstream.read (buffer) > 0) {outputstream.write (buffer, 0, length);  
        } inputstream.close ();  
    Outputstream.close (); } public void DisplayContact (Cursor c) {Toast.maketext (this, "ID:" + c.ge  
          Tstring (0) + "\ n" + "Name:" + c.getstring (1) + "\ n" + "Email:" + c.getstring (2),      Toast.length_long). Show (); }  
}

3. Debug, with DDMS view, the database is copied to the specified location.

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.