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

举报
再见孙悟空_ 发表于 2022/01/14 00:28:46 2022/01/14
4.3k+ 0 0
【摘要】 /** *检查某表是否存在 * @param tableName 表名 * @return true:存在 false:不存在 */ public boolean tabIsExist(String tabName){ boolean result = false; if(t...
/**
 

          *检查某表是否存在
          * @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 ;
          }
  
 

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/72764845

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。