Android SQLite数据库判断某张表是否存在,是否存在数据的方法

最近数据库用的比较多,所以就记录一下,以备未来使用

废话不多说,直接上代码

  public static boolean HaveData(SQLiteDatabase db,String tablename){
        Cursor cursor;
        boolean a=false;
        cursor = db.rawQuery("select name from sqlite_master where type='table' ", null);
        while(cursor.moveToNext()){
            //遍历出表名
            String name = cursor.getString(0);
            if(name.equals(tablename))
            {
                a=true;
            }
            Log.i("System.out", name);
        }
        if(a)
        {
            cursor=db.query(tablename,null,null,null,null,null,null);
            //检查是不是空表
            if(cursor.getCount()>0)
                return true;
            else
                return false;
        }
        else
            return false;

    }
发布了28 篇原创文章 · 获赞 49 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qwe25878/article/details/80496857