/** * Check if the specified conditions data already exists in the table. * @param modelClass * Which table to check by class. * @param conditions * A filter declaring which data to check. Exactly same use as * {@link Operator#where(String...)}, except null conditions will result in false. * @return Return true if the specified conditions data already exists in the table. * False otherwise. Null conditions will result in false. */ public static <T> boolean isExist(Class<T> modelClass, String... conditions) { return conditions != null && where(conditions).count(modelClass) > 0; }
/** * Count the records. * * <pre> * LitePal.count(Person.class); * </pre> * * This will count all rows in person table.<br> * You can also specify a where clause when counting. * * <pre> * LitePal.where("age > ?", "15").count(Person.class); * </pre> * * @param modelClass * Which table to query from by class. * @return Count of the specified table. */ public int count(Class<?> modelClass) { return count(BaseUtility.changeCase(modelClass.getSimpleName())); }
@Override public void onClick(View view) { int result = 0; switch (view.getId()) { case R.id.count_btn1: result = LitePal.count(Singer.class); mResultText.setText(String.valueOf(result)); break; case R.id.count_btn2: try { result = LitePal.where("age > ?", mAgeEdit.getText().toString()).count( Singer.class); mResultText.setText(String.valueOf(result)); } catch (Exception e) { e.printStackTrace(); } break; default: } }
/** * Count the records. * * <pre> * LitePal.count("person"); * </pre> * * This will count all rows in person table.<br> * You can also specify a where clause when counting. * * <pre> * LitePal.where("age > ?", "15").count("person"); * </pre> * * @param tableName * Which table to query from. * @return Count of the specified table. */ public static int count(String tableName) { synchronized (LitePalSupport.class) { FluentQuery cQuery = new FluentQuery(); return cQuery.count(tableName); } }
@Override public void run() { synchronized (LitePalSupport.class) { final int count = count(tableName); if (executor.getListener() != null) { Operator.getHandler().post(new Runnable() { @Override public void run() { executor.getListener().onFinish(count); } }); } } } };
@Test public void testCount() { int result = LitePal.count(Student.class); int realResult = -100; Cursor cursor = LitePal.findBySQL("select count(1) from " + studentTable); if (cursor.moveToFirst()) { realResult = cursor.getInt(0); } cursor.close(); assertEquals(realResult, result); result = LitePal.where("id > ?", "99").count(studentTable); cursor = LitePal.findBySQL("select count(1) from " + studentTable + " where id > ?", "99"); if (cursor.moveToFirst()) { realResult = cursor.getInt(0); } cursor.close(); assertEquals(realResult, result); try { LitePal.count("nosuchtable"); fail(); } catch (Exception e) { } }