前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android sqlite 判断表和表中字段是否存在方法

android sqlite 判断表和表中字段是否存在方法

作者头像
再见孙悟空_
发布2023-02-10 19:43:17
1.7K0
发布2023-02-10 19:43:17
举报
代码语言:javascript
复制
/**
代码语言:javascript
复制
    *检查某表是否存在
    * @param tableName 表名
    * @return  true:存在  false:不存在
    */


public boolean tabIsExist(String tabName){
        boolean result = false;
        if(tabName == null){
                return false;
        }
        Cursor cursor = null;
        try {
               
                String sql = "select count(*) as c from sqlite_master where type ='table' and name ='"+tabName.trim()+"' ";
                cursor = mUDB.rawQuery(sql, null);
                if(cursor.moveToNext()){
                        int count = cursor.getInt(0);
                        if(count>0){
                                result = true;
                        }
                }
                
        } catch (Exception e) {
        }                
        return result;
}


/**
    *检查表中某列是否存在
    * @param db
    * @param tableName 表名
    * @param columnName 列名
    * @return  true:存在  false:不存在
    */
    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("","checkColumnExists2..." + e.getMessage()) ;
        }finally{
            if(null != cursor && !cursor.isClosed()){
                cursor.close() ;
            }
        }


        return result ;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档