A good database base class, which facilitates the construction of database operation classes.

Source: Internet
Author: User

Android uses sqlite as a database. sqlite is quite easy to use. Of course, it also encapsulates it. However, I still like to write it using SQL instead of calling android-encapsulated functions, in this way, it can be reused on different platforms.

Code: The following is a base class of a Database. Using this class, you can quickly construct a database operation class. When writing a database, I use trycatch in my experience. This makes it easy to find a problem.

Import android. content. Context;
Import android. database. Cursor;
Import android. database. SQLException;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
Import android. util. Log;

Public abstract class AbstractDBHelper
{
// SQLite database instance
Protected SQLiteDatabase mDb = null;

// Database creation helper
Protected CreateDBHelper mDbHelper = null;

// Obtain the ID of the current database helper class (generally the class name), used for logging and other records
Protected abstract String getTag ();

// Obtain the Database Name
Protected abstract String getDatabaseName ();

/**
* Obtain the database version. The value must be at least 1.
* When the database structure changes, add this value to 1 and the system will automatically call it during initialization.
* The createDBTables and dropDBTables Methods update the database structure.
*/
Protected abstract int getDatabaseVersion ();

// Create an SQL statement for the database table. One element and one statement
Protected abstract String [] createDBTables ();

// Delete the SQL statement of the database table, one element and one statement
Protected abstract String [] dropDBTables ();

// Helper class for internal database creation
Private class CreateDBHelper extends SQLiteOpenHelper
{
Public CreateDBHelper (Context ctx)
{
Super (ctx, getDatabaseName (), null, getDatabaseVersion ());
}
@ Override
Public void onCreate (SQLiteDatabase db)
{
ExecuteBatch (createDBTables (), db );
}
 
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
{
Log. w (getTag (), "Upgrading database '" + getDatabaseName () + "'from version" + oldVersion + "to" + newVersion );
ExecuteBatch (dropDBTables (), db );
OnCreate (db );
}
 
// Execute SQL statements in batches
Private void executeBatch (String [] sqls, SQLiteDatabase db)
{
If (sqls = null)
{
Return;
}

Db. beginTransaction ();
Try
{
Int len = sqls. length;
For (int I = 0; I <len; I ++)
{
Db.exe cSQL (sqls [I]);
}
Db. setTransactionSuccessful ();
}
Catch (Exception e)
{
Log. e (getTag (), e. getMessage (), e );
}
Finally
{
Db. endTransaction ();
}
}
}

// Open or create a database with the specified name
Public void open (Context ctx)
{
Log. I (getTag (), "Open database" + getDatabaseName () + "'");
Try
{
MDbHelper = new CreateDBHelper (ctx );
If (mDbHelper! = Null)
{
MDb = mDbHelper. getWritableDatabase ();
}
}
Catch (SQLException e)
{
Log. e ("open", e. getMessage ());
}

}

// Close the database
Public void close ()
{
Try
{
If (mDbHelper! = Null)
{
Log. I (getTag (), "Close databas'" + getDatabaseName () + "'");
MDbHelper. close ();
}
}
Catch (SQLException e)
{
Log. e ("close", e. getMessage ());
}
}
 
// Execute SQL
Protected void executeSQL (String SQL)
{
If (SQL. equals (""))
{
Return;
}

Try
{
MDb.exe cSQL (SQL );
}
Catch (SQLException e)
{
Log. I (getTag (), e. getMessage (), e );
}
}

Protected Cursor Query (String SQL, String [] strArgs)
{
Return mDb. rawQuery (SQL, strArgs );
}
}

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.