SQLite Database判断是否存在、新建、删除.....

 Android SQLite Database本打算封装SQLite的,但是,发现实际上Android已经封装了一层,尤其是数据的增、删、查、改上,更是如此,所以,我后来只封装了一个连接层,负责打开、建立数据库,并返回一个SQLite的实例用来完成增、删、插、改等操作。

//----------- DBHelper -------------

package utility.db;

import java.io.FileNotFoundException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBHelper
{
private SQLiteDatabase db = null;

private Context ctx = null;

public DBHelper( Context ctx )
{
this.ctx = ctx;
}

/**
* 判断数据库是否存在
*
* @param dbName
* 数据库名称
* @return true存在 false不存在
*/
public boolean exist( String dbName )
{
Log.log("called fun : exist()");
boolean flag = false;
try
{
db = ctx.openDatabase(dbName, null);
Log.log("database/"+dbName+" exist");
flag = true;
}
catch (FileNotFoundException e)
{
flag = false;
Log.log("database/"+dbName+" not exist");
}
finally
{
if (db != null)
db.close();
db = null;
}
return flag;
}

/**
* 打开数据库
*
* @param dbName
* 数据库名称
* @return true 打开成功 false打开失败
*/
public boolean openDatabase( String dbName , boolean created )
{
return openDatabase(dbName, null, created);
}

/**
* 打开数据库
*
* @param dbName
* 数据库名称
* @param factory
* @return true 打开成功 false打开失败
*/
public boolean openDatabase( String dbName , CursorFactory factory ,
boolean created )
{
Log.log("called fun : openDatabase()");
Log.log("called fun : if not exist then created "+dbName+" ? : "+created);
boolean flag = false;
if(exist(dbName))
{
try
{
db = ctx.openDatabase(dbName, factory);
Log.log("database/"+dbName+" is opened");
flag = true;
}
catch (FileNotFoundException e)
{
Log.log("database/"+dbName+" is not opened");
flag = false;
}
}
else
{
if(created)
{
flag = createDatabase(dbName, 1, 0, factory);
}
else
{
flag = false;
}
}
return flag;

}

/**
* 创建数据库
*
* @param dbName
* 数据库名称
* @param version
* 数据库版本
* @param mode
* 模式
* @param factory
* @return true 建立成功 false 建立失败
*/
public boolean createDatabase( String dbName , int version , int mode ,
CursorFactory factory )
{
Log.log("called fun : createDatabase()");
boolean flag = false;
try
{
db = ctx.createDatabase(dbName, version, mode, factory);
Log.log("database/"+dbName+" is created successfully!");
flag = true;
}
catch (FileNotFoundException e)
{
Log.log("database/"+dbName+" can't be created");
flag = true;
}
return flag;
}

/**
* 建立数据库
*
* @param dbName
* 数据库名称
* @return true 打开成功 false 打开失败
*/
public boolean createDatabase( String dbName )
{
return createDatabase(dbName);
}

/**
* 关闭数据库
*
*/
public void closeDatabase()
{
Log.log("close database");
if (db != null)
db.close();
db = null;
ctx = null;
}
/**
* 删除表
* @param tableName 表名
* @return 成功则返回 表名 失败则返回null
*/
public String dropTable( String tableName )
{
Log.log("called fun dropTable()");
if (tableName == null)
{
Log.log("table name is error! "+tableName+" is null !");
return null;
}
String DROP_TABLE = "DROP TABLE IF EXISTS " + tableName;
db.execSQL(DROP_TABLE);
Log.log("table/"+tableName+" is dropped successfully");
return tableName;
}
/**
* 取得数据库实例
* @return 返回数据库实例
*/
public SQLiteDatabase getDBInstance()
{
return db;
}
public boolean execSQL(String SQL)
{
boolean flag = false;
Log.log("called fun execSQL()");
Log.log("SQL:"+SQL);
try
{
db.execSQL(SQL);
Log.log("sql exec successfully!");
flag = true;
}
catch(Exception e)
{
Log.log("sql exec not successfully!");
flag = false;
}
return flag;
}

}

  

你可能感兴趣的:(数据库,sqlite,String,null,database,fun)