Android database SQlite

Source: Internet
Author: User
I have been engaged in Android development for a long time, but I have not yet written the basics of sqlite, which will be expanded later...

 

Package com. king. android. db;

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

/**

* Description: database...
* Author: Andy. Liu
* Time: 10:37:27
**/
Public class MyDataBaseAdapter {

Private static final String TAG = "MyDataBaseAdapter ";

Private Context mContext = null;

Public static final String KEY_ID = "_ id ";

Public static final String KEY_NUM = "num ";

Public static final String KEY_DATA = "data ";

Private static final String DB_NAME = "king. db ";

Private static final String TABLE_KING = "king ";

Private static final int DB_VERSION = 1;


// Create a table statement
Private static final String CREATE_TAB_KING = null;

// Execute open () to open the database and save the Returned Database Object
Private SQLiteDatabase mSQlLiteDatabase = null;

// Inherited by SQLiteOpenHelper
Private DatabaseHelper mDatabaseHelper = null;

// Inherit from SQLiteOpenHelper
Private static class DatabaseHelper extends SQLiteOpenHelper {

// Create a database using the constructor
Public DatabaseHelper (Context context ){
/**
* When getWriteableDatabase () and getReadableDatabase () are called, a database is created.
*/
Super (context, DB_NAME, null, DB_VERSION );

}

@ Override/* create a data table */
Public void onCreate (SQLiteDatabase db ){
Db.exe cSQL (CREATE_TAB_KING );
}

@ Override // upgrade the database
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
Db.exe cSQL ("drop table if exists notes ");
OnCreate (db );
}
}
// The constructor obtains the Context
Public MyDataBaseAdapter (Context context ){
This. mContext = context;
}

// Open the database and return the database object
Public void open () throws SQLException {
MDatabaseHelper = new DatabaseHelper (mContext );
MSQlLiteDatabase = mDatabaseHelper. getWritableDatabase ();
}

// Close the database
Public void close (){
MDatabaseHelper. close ();
}

// Insert a data entry
Public long insertData (int num, String data ){
ContentValues initValues = new ContentValues ();
InitValues. put (KEY_NUM, num );
InitValues. put (KEY_DATA, data );
Return mSQlLiteDatabase. insert (TABLE_KING, KEY_ID, initValues );
}

// Delete a piece of data
Public boolean deleteData (long rowId ){
Return mSQlLiteDatabase. delete (TABLE_KING, KEY_ID + "=" + rowId, null)> 0;
}

// Query all data through Cursor
Public Cursor fetchAllData (){
Return mSQlLiteDatabase. query (TABLE_KING, new String [] {KEY_ID, KEY_NUM, KEY_DATA}, null );
}

// Query the specified data
Public Cursor fetchData (long rowId ){
Cursor mCursor = mSQlLiteDatabase. query (true, TABLE_KING, new String [] {KEY_ID, KEY_NUM, KEY_DATA}, KEY_ID + "=" + rowId, null, null );
If (null! = MCursor ){
MCursor. moveToFirst ();
}
Return mCursor;
}

// Update a piece of data
Public boolean updateData (long rowId, int num, String data ){
ContentValues args = new ContentValues ();
Args. put (KEY_NUM, num );
Args. put (KEY_DATA, data );
Return mSQlLiteDatabase. update (TABLE_KING, args, KEY_ID + "=" + rowId, null)> 0;
}

}

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.