Working with the android Database

Source: Internet
Author: User

A good habit is to create a helper class to simplify your database interaction.

 

Consider creating a database adapter to add a packaging layer that interacts with the database. It should provide intuitive and strong-type methods, such as adding, deleting, and updating projects. The database Adapter should also process queries and create, open, and close database packaging.

 

It also uses static database constants to define the table name, column name, and column index.

 

The following code snippet shows a framework of the standard database adapter class. It includes an extension class of the sqliteopenhelper class to simplify opening, creating, and updating databases.

 

Import Android. content. context;

Import Android. database .*;

Import Android. database. SQLite .*;

Import Android. database. SQLite. sqlitedatabase. cursorfactory;

Import Android. util. log;

Public class mydbadapter

{

Private Static final string database_name = "mydatabase. DB ";

Private Static final string database_table = "maintable ";

Private Static final int database_version = 1;

 

// The index (key) column name for use in where clauses.

Public static final string key_id = "_ ID ";

 

// The name and column index of each column in your database.

Public static final string key_name = "name ";

Public static final int name_column = 1;

 

// Todo: Create public field for each column in your table.

// SQL statement to create a new database.

Private Static final string database_create = "create table" +

Database_table + "(" + key_id + "integer primary key autoincrement," +

Key_name + "text not null );";

 

// Variable to hold the database instance

Private sqlitedatabase dB;

 

// Context of the application using the database.

Private Final context;

 

// Database open/upgrade helper

Private mydbhelper dbhelper;

 

Public mydbadapter (context _ context ){

Context = _ context;

Dbhelper = new mydbhelper (context, database_name, null, database_version );

}

 

Public mydbadapter open () throws sqlexception {

DB = dbhelper. getwritabledatabase ();

Return this;

}

 

Public void close (){

DB. Close ();

}

 

Public long insertentry (myobject _ myobject ){

Contentvalues = new contentvalues ();

// Todo fill in contentvalues to represent the new row

Return dB. insert (database_table, null, contentvalues );

}

 

Public Boolean removeentry (long _ rowindex ){

Return dB. Delete (database_table, key_id + "=" + _ rowindex, null)> 0;

}

Public cursor getallentries (){

Return dB. Query (database_table, new string [] {key_id, key_name },

Null, null, null );

}

Public myobject getentry (long _ rowindex ){

Myobject objectinstance = new myobject ();

// Todo return a cursor to a row from the database and

// Use the values to populate an instance of myobject

Return objectinstance;

}

Public int updateentry (long _ rowindex, myobject _ myobject ){

String where = key_id + "=" + _ rowindex;

Contentvalues = new contentvalues ();

// Todo fill in the contentvalue based on the new object

Return dB. Update (database_table, contentvalues, where, null );

}

 

Private Static class mydbhelper extends sqliteopenhelper

{

Public mydbhelper (context, string name, cursorfactory factory, int version ){

Super (context, name, factory, version );

}

 

// Called when no database exists in

// Disk and the helper class needs

// To create a new one.

@ Override

Public void oncreate (sqlitedatabase _ dB ){

_Db.exe csql (database_create );

}

 

// Called when there is a database version mismatch meaning that

// The version of the database on disk needs to be upgraded

// The current version.

@ Override

Public void onupgrade (sqlitedatabase _ dB, int _ oldversion, int _ newversion ){

// Log the version upgrade.

Log. W ("taskdbadapter", "upgrading from version" +

_ Oldversion + "to" + _ newversion +

", Which will destroy all old data ");

// Upgrade the existing database to conform to the new version.

// Multiple previous versions can be handled by comparing

// _ Oldversion and _ newversion values.

// The simplest case is to drop the old table and create

// New one.

_Db.exe csql ("Drop table if exists" + database_table );

 

// Create a new one.

Oncreate (_ dB );

}

}

}

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.