android 对象是否存在,android 检测数据库表中字段(列)是否存在以及添加列

一般数据库升级时,或者为满足需求新增加某些字段,需要检测表中是否已存在相应字段(列),因为列名重复会报错。方法有很多,下面列举2种常见的方式:

1、根据 cursor.getColumnIndex(String columnName) 的返回值判断,如果为-1表示表中无此字段/**

* 方法1:检查某表列是否存在

* @param db

* @param tableName 表名

* @param columnName 列名

* @return 布尔值结果

*/

private boolean isColumnExist(SQLiteDatabase db, String tableName

, String columnName) {

boolean result = false ;

Cursor cursor = null ;

try{

//查询一行

cursor = db.rawQuery( "SELECT * FROM " + tableName + " LIMIT 0"

, null );

result = cursor != null && cursor.getColumnIndex(columnName) != -1 ;

}catch (Exception e){

Log.e(TAG,"checkColumnExists1..." + e.getMessage()) ;

}finally{

if(null != cursor && !cursor.isClosed()){

cursor.close() ;

}

}

return result ;

}

2、通过查询sqlite的系统表 sqlite_master 来查找相应表里是否存在该字段,从判断表是否存在改过来的/**

* 方法2:检查表中某列是否存在

* @param db

* @param tableName 表名

* @param columnName 列名

* @return 布尔值结果

*/

private boolean isColumnExists(SQLiteDatabase db, String tableName

, String columnName) {

boolean result = false ;

Cursor cursor = null ;

try{

cursor = db.rawQuery( "select * from sqlite_master where name = ? and sql like ?"

, new String[]{tableName , "%" + columnName + "%"} );

result = null != cursor && cursor.moveToFirst() ;

}catch (Exception e){

Log.e(TAG,"checkColumnExists2..." + e.getMessage()) ;

}finally{

if(null != cursor && !cursor.isClosed()){

cursor.close() ;

}

}

return result ;

}

在项目开发中使用的情况:往往是需要添加列的/**

* 只适合于在后面加一个int字段

*

* @return true 表示已经有了该字段,是最新版的数据库,false 反之..

*/

public boolean checkTableCul(String tableName, String culName) {

boolean result = false;

Cursor cursor = null;

try {

HashMap maps = new HashMap();

maps.put("[" + culName + "]", " int DEFAULT (0)");    // DEFAULT (0)

cursor = getDataBase().rawQuery("select sql from sqlite_master where type='table' and " +

"name=?", new String[]{tableName});

if (cursor != null && cursor.moveToFirst()) {

String sql = cursor.getString(0);

if (!StringUtils.isNullOrEmpty(sql)) {

Iterator ite = maps.keySet().iterator();

while (ite.hasNext()) {

String st = ite.next() + " ";

if (sql.contains(st)) {

ite.remove();

}

}

}

}

if (!maps.keySet().contains("[" + culName + "]")) {

result = true;

}

for (String st : maps.keySet()) {

addCol(getDataBase(), tableName, st, maps.get(st));

}

} catch (Exception e) {

LogManager.e("oncreatedb", e);

} finally {

if (cursor != null) {

cursor.close();

cursor = null;

}

}

return result;

}

void addCol(SQLiteDatabase db, String tableName, String col, String type) {

if (db != null) {

db.execSQL(new StringBuffer().append("alter table ").append(tableName)

.append(" add ").append(col).append(" ").append(type).toString());

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值