package
com.example.transportphone;
import
android.content.Context;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteOpenHelper;
import
android.util.Log;
public
class
DataBase {
public
static
final
String TAG =
"Database"
;
public
static
final
String DB_name =
"INFO"
;
/**
* 싱글톤 인스턴스
*/
private
static
DataBase database;
/**
* table name for information of registers
*/
public
static
String TABLE_INFO =
"INFO"
;
/**
* version
*/
public
static
int
DATABASE_VERSION =
1
;
/**
* Helper class defined
*/
private
DatabaseHelper dbHelper;
/**
* SQLiteDatabase 인스턴스
*/
protected
SQLiteDatabase db;
/**
* 컨텍스트 객체
*/
private
Context context;
/**
* 생성자
*/
private
DataBase(Context context) {
this
.context = context;
}
/**
* 인스턴스 가져오기
*/
public
static
DataBase getInstance(Context context) {
if
(database ==
null
) {
database =
new
DataBase(context);
}
return
database;
}
/**
* 데이터베이스 열기
*/
public
boolean
open() {
println(
"opening database ["
+ DB_name +
"]."
);
dbHelper =
new
DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
return
true
;
}
/**
* 데이터베이스 닫기
*/
public
void
close() {
println(
"closing database ["
+ DB_name +
"]."
);
db.close();
database =
null
;
}
/**
* execute raw query using the input SQL
* close the cursor after fetching any result
*
* @param SQL
* @return
*/
public
Cursor rawQuery(String SQL) {
println(
"\nexecuteQuery called.\n"
);
Cursor c1 =
null
;
try
{
c1 = db.rawQuery(SQL,
null
);
println(
"cursor count : "
+ c1.getCount());
}
catch
(Exception ex) {
Log.e(TAG,
"Exception in executeQuery"
, ex);
}
return
c1;
}
public
boolean
execSQL(String SQL) {
println(
"\nexecute called.\n"
);
try
{
Log.d(TAG,
"SQL : "
+ SQL);
db.execSQL(SQL);
}
catch
(Exception ex) {
Log.e(TAG,
"Exception in executeQuery"
, ex);
return
false
;
}
return
true
;
}
/**
* Database Helper inner class
*/
private
class
DatabaseHelper
extends
SQLiteOpenHelper {
public
DatabaseHelper(Context context) {
super
(context, DB_name,
null
, DATABASE_VERSION);
}
public
void
onCreate(SQLiteDatabase db) {
db = db.openOrCreateDatabase(
"info.s3db"
,
null
);
println(
"creating database ["
+ DB_name +
"]."
);
println(
"creating table ["
+ TABLE_INFO +
"]."
);
String DROP_SQL =
"drop table if exists "
+ TABLE_INFO;
try
{
db.execSQL(DROP_SQL);
}
catch
(Exception ex) {
Log.e(TAG,
"Exception in DROP_SQL"
, ex);
}
String CREATE_SQL =
"create table "
+ TABLE_INFO +
"("
+
" _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
+
" name TEXT NOT NULL, "
+
" phonenumber TEXT NOT NULL, "
+
");"
;
try
{
db.execSQL(CREATE_SQL);
}
catch
(Exception ex) {
Log.e(TAG,
"Exception in CREATE_SQL"
, ex);
}
try
{
db.execSQL(DROP_SQL);
}
catch
(Exception ex) {
Log.e(TAG,
"Exception in DROP_SQL"
, ex);
}
}
public
void
onOpen(SQLiteDatabase db)
{
println(
"opened database ["
+ DB_name +
"]."
);
}
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion)
{
println(
"Upgrading database from version "
+ oldVersion +
" to "
+ newVersion +
"."
);
}
}
private
void
println(String msg) {
Log.d(TAG, msg);
}
}