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.