The method currently described is only used to store some simple data. If you want to store relational data, it will be more efficient to use the database. For example, if you want to store the scores of every student in the school, it's best to use the database, because you can query a student's specific score. Furthermore, the use of a database can preserve the integrity of relationships between different data.
Android uses the SQLite database system. With this database, only the program that created it can use it, and other programs cannot access it.
In the next few sections, you'll be briefed on how to create a SQLite database in your program by encoding. For Android, the storage location of a database created by encoding is/data/data/<packane_name>
/databases.
A good way to work with a database is to create a database helper class that encapsulates all of the data manipulation methods. So, in this section, let's look at how to create a database helper class Dbadapter, which includes creating, opening, closing, and using the SQLite database.
Let's talk about creating a database named MyDB, including a table, named Contacts, with 3 columns of _id,name and email.
1. Create a project, Databases.
2. Create a class, Dbadater.
3. Code in the Dbadapter.java.
public class Dbadapter {static final String Key_rowid = "_id";
Static final String key_name = "NAME";
Static final String Key_email = "EMAIL";
Static final String TAG = "Dbadapter";
Static final String database_name = "MyDB";
Static final String database_table = "Contacts";
static final int database_version = 2; Static final String database_create = "CREATE table Contacts (_id Integer primary key autoincrement," +
' Name text NOT NULL ', email text not null; ';
Final context context;
Databasehelper DBHelper;
Sqlitedatabase DB;
Public Dbadapter (Context ctx) {this.context = CTX;
DBHelper = new Databasehelper (context); private static class Databasehelper extends Sqliteopenhelper {databasehelper (Context Conte
XT) {Super (context, database_name, NULL, database_version); @Override public void OnCreate (Sqlitedatabase db) {try {
Db.execsql (database_create);
catch (SQLException e) {e.printstacktrace ();
@Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {LOG.W (TAG, "Upgrading Database from Version" + Oldversion + "to" + Newver
Sion + ", which'll destroy all old data");
Db.execsql ("DROP TABLE IF EXISTS contacts");
OnCreate (DB); }//---Opens the database---public dbadapter open () throws SQLException {db
= Dbhelper.getwritabledatabase ();
return this;
}//---Closes the database---public void close () {dbhelper.close ();
//---Insert a contacts into the database---Public long Insertcontact (string name, string email) {Contentvalues initialvalues = new Contentvalues ();
Initialvalues.put (Key_name, NAME);
Initialvalues.put (Key_email, EMAIL);
Return Db.insert (database_table, NULL, initialvalues); }//---Deletes a particular contacts---public boolean deletecontact (long rowId) {Retu
RN Db.delete (database_table, key_rowid + "=" + ROWID, NULL) > 0; }//---Retrieves all of the contacts---public Cursor getallcontacts () {return db.query (D
Atabase_table, new string[] {key_rowid, key_name, key_email}, NULL, NULL, NULL, NULL, NULL);
}//---Retrieves a particular contacts---public Cursor getcontact (long rowId) throws SQLException
{Cursor Mcursor = Db.query (True, database_table, new string[] {key_rowid, Key_name, Key_email}, Key_rowid +"=" + rowId, NULL, NULL, NULL, NULL, NULL);
if (mcursor!= null) {Mcursor.movetofirst ();
return mcursor;
//---Updates a contacts---public boolean updatecontact (long rowId, string name, string email)
{Contentvalues args = new Contentvalues ();
Args.put (Key_name, NAME);
Args.put (Key_email, EMAIL);
Return Db.update (database_table, args, key_rowid + "=" + ROWID, NULL) > 0; }
}