SQLite查找表是否有时有记录

来源:互联网 发布:电脑软件隐藏 编辑:程序博客网 时间:2024/05/23 15:02


 SQLiteDatabase db= SQLiteDatabase.openOrCreateDatabase(this.getFilesDir()+"/Diary_aks.db3", null);//在Oncreate中打开数据库Diary_aks.db3

if(tabIsExist("User")){//查找表USer是不为空 则跳转到 LoginActivity
intent=new Intent(IndexActivity.this,LoginActivity.class); 
}
else{//查找表USer为空则跳转到 RegistActivity
intent=new Intent(IndexActivity.this,RegistActivity.class);
}
 startActivity(intent);
}

/**
* 判断表tabName是否为空
* @param tabName 表名
* @return  boolean
*/

       public boolean tabIsExist(String tabName){

        boolean result = false;
        if(tabName == null){
                return false;
        }
        Cursor cursor = null;
        try {
      
                DataBaseOpenHelper dbh=new DataBaseOpenHelper(IndexActivity.this);//
                String sql = "select count(*) from User ";//USer表名
                cursor = dbh.getReadableDatabase().rawQuery(sql, null);
                if(cursor.moveToNext()){
                        int count = cursor.getInt(0);
                        if(count>0){
                                result = true;
                        }
                }
                
        } catch (Exception e) {
                // TODO: handle exception
        }                
        return result;
   }
原创粉丝点击