Android database SQLITE (used by SQLITEOPENHELPER)

Source: Internet
Author: User

 
---------------------------------------------. Java
Package com. example. sqlite;
 
 
Import android. app. Activity;
Import android. database. sqlite. SQLiteDatabase;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;
 
 
Public class MainActivity extends
Activity {
Button mButton1, mButton2,
MButton3;
MyDbHelper dbhelper;
SQLiteDatabase sqLiteDatabase;
 
 
Private static final int old_VERSION = 1;
Private static final int new_VERSION = 2;
 
 
@ Override
Public void onCreate (
Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MButton1 = (Button) findViewById (R. id. button1 );
MButton2 = (Button) findViewById (R. id. button2 );
MButton3 = (Button) findViewById (R. id. button3 );
 
 
MButton1.setOnClickListener (new OnClickListener (){
 
 
Public void onClick (View v ){
// SqLiteDatabase = openOrCreateDatabase (
// DBNAME,
// Context. MODE_PRIVATE,
// Null );
//
//
// If (sqLiteDatabase! = Null ){
//
// Toast. makeText (getBaseContext (), "created successfully" + DBNAME, 2000). show ();
//}
// Else {
// Toast. makeText (getBaseContext (), "failed to create" + DBNAME, 2000). show ();
//}
//
 
Dbhelper = new MyDbHelper (getBaseContext (), old_VERSION );
 
SqLiteDatabase = dbhelper. getWritableDatabase ();
 
Toast. makeText (getBaseContext (), "created successfully", 2000). show ();
}
});
 
MButton2.setOnClickListener (new OnClickListener (){
 
Public void onClick (View v ){
// TODO Auto-generated method stub
// SqLiteDatabase. close ();
 
 
Dbhelper = new MyDbHelper (getBaseContext (), new_VERSION );
SqLiteDatabase = dbhelper. getWritableDatabase ();
 
Toast. makeText (getBaseContext (), "updated successfully", 2000). show ();
}
});
 
// MButton3.setOnClickListener (new OnClickListener (){
//
// Public void onClick (View v ){
/// TODO Auto-generated method stub
//
// Boolean isDeleted = deleteDatabase (DBNAME );
//
// If (isDeleted ){
//
//
//
// Toast. makeText (getBaseContext (), "deleted Database" + DBNAME, 2000). show ();
//}
// Else {
// Toast. makeText (getBaseContext (), "failed to delete database" + DBNAME, 2000). show ();
//}
//}
//});
}
@ Override
Protected void onDestroy (){
// TODO Auto-generated method stub
Super. onDestroy ();
Dbhelper. close ();
}
 
 
@ Override
Public boolean onCreateOptionsMenu (
Menu menu ){
GetMenuInflater (). inflate (
R. menu. activity_main,
Menu );
Return true;
}
}
 
 
--------------------------------- MyDbHelper. java
Package com. example. sqlite;
 
 
Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
Import android. util. Log;
 
 
Public class MyDbHelper extends SQLiteOpenHelper {
 
 
Private static final String DB_NAME = "db_2.db3 ";
Private static final String TAG = "LOOK ";
Public MyDbHelper (Context context,
 
Int version ){
Super (context, DB_NAME, null, version );
// TODO Auto-generated constructor stub
}
 
 
@ Override
Public void onCreate (
SQLiteDatabase db ){
// TODO Auto-generated method stu"
Log. d (TAG, "Begin execute onCreate ");
 
Try {
Db. beginTransaction ();
String sqlString = "create table TBL_USER (_ id integer primary key, name text not null )";
Db.exe cSQL (sqlString );
 
 
Db. setTransactionSuccessful ();
 
}
Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
Finally
{
Db. endTransaction ();}
 
Log. d (TAG, "execute onCreate Completed .");
}
 
 
@ Override
Public void onUpgrade (
SQLiteDatabase db,
Int oldVersion,
Int newVersion ){
// TODO Auto-generated method stub
 
 
Log. d (TAG, "Begin execute onUpgrade ");
Try {
Db. beginTransaction ();
 
String upgradeString = "alter table TBL_USER add column created_dt timestamp not null default '2017-09-07 'collate nocase ;";
Db.exe cSQL (upgradeString );
Db. setTransactionSuccessful ();
}
Catch (Exception e ){
// TODO: handle exception
 
E. printStackTrace ();
}
Finally {
Db. endTransaction ();
}
 
 
Log. d (TAG, "execute onUpgrade Completed ");
 
}
 
 

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.