android 怎么判断数据库里没有数据

android 怎么判断数据库里没有数据,第1张

Cursor cur = DR.query("hellokitty", new String[]{"hello"}, null, null, null, null,null)

if(!cur.moveToFirst()){

//没有数据

}

我是这么做的,在hellokitty的表里找hello数据,如果!cur.moveToFirst 就表明没有数据。。否则就存在数据

sqlite判断表是否存在,代码如下:

/**

         * 判断某张表是否存在

         * @param tabName 表名

         * @return

         */

        public boolean tabIsExist(String tabName){

                boolean result = false

                if(tabName == null){

                        return false

                }

                SQLiteDatabase db = null

                Cursor cursor = null

                try {

                        db = this.getReadableDatabase()//此this是继承SQLiteOpenHelper类得到的

                        String sql = select count(*) as c from sqlite_master where type ='table' and name ='+tabName.trim()+' 

                        cursor = db.rawQuery(sql, null)

                        if(cursor.moveToNext()){

                                int count = cursor.getInt(0)

                                if(count>0){

                                        result = true

                                }

                        }

                         

                } catch (Exception e) {

                        // TODO: handle exception

                }                

                return result

        }


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9993760.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存