当前位置:Gxlcms > 数据库问题 > sqlite 查询表和字段是否存在

sqlite 查询表和字段是否存在

时间:2021-07-01 10:21:17 帮助过:1人阅读

2、通过查询sqlite的系统表 sqlite_master 来查找相应表里是否存在该字段,稍微换下语句也可以查找表是否存在

/**
* 方法2:检查表中某列是否存在
* @param db
* @param tableName 表名
* @param columnName 列名
* @return
*/
private boolean checkColumnExists2(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 ;
}


我用的方法2,然后用alter改了表结构后,一样可以查出来
alter table yuhuolixian add aaa integer null
select * from sqlite_master where name = ‘yuhuolixian‘ and sql like ‘%aaa%‘

sqlite 查询表和字段是否存在

标签:

人气教程排行