Java Code Examples for org.litepal.LitePal#findBySQL()

The following examples show how to use org.litepal.LitePal#findBySQL() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryMathTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@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) {
	}
}
 
Example 2
Source File: QueryMathTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testAverage() {
	double result = LitePal.average(Student.class, "age");
	double realResult = -100;
	Cursor cursor = LitePal.findBySQL("select avg(age) from " + studentTable);
	if (cursor.moveToFirst()) {
		realResult = cursor.getDouble(0);
	}
	cursor.close();
	assertEquals(realResult, result);
	result = LitePal.where("id > ?", "99").average(studentTable, "age");
	cursor = LitePal.findBySQL("select avg(age) from " + studentTable + " where id > ?", "99");
	if (cursor.moveToFirst()) {
		realResult = cursor.getDouble(0);
	}
	cursor.close();
	assertEquals(realResult, result);
	try {
           LitePal.average(Student.class, "nosuchcolumn");
		fail();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 3
Source File: QueryMathTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testMax() {
	int result = LitePal.max(Student.class, "age", Integer.TYPE);
	int realResult = -100;
	Cursor cursor = LitePal.findBySQL("select max(age) from " + studentTable);
	if (cursor.moveToFirst()) {
		realResult = cursor.getInt(0);
	}
	cursor.close();
	assertEquals(realResult, result);
	result = LitePal.where("age < ?", "20").max(studentTable, "age", Integer.TYPE);
	cursor = LitePal.findBySQL("select max(age) from " + studentTable + " where age < ?", "20");
	if (cursor.moveToFirst()) {
		realResult = cursor.getInt(0);
	}
	cursor.close();
	assertEquals(realResult, result);
}
 
Example 4
Source File: QueryMathTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testMin() {
	int result = LitePal.min(Student.class, "age", Integer.TYPE);
	int realResult = -100;
	Cursor cursor = LitePal.findBySQL("select min(age) from " + studentTable);
	if (cursor.moveToFirst()) {
		realResult = cursor.getInt(0);
	}
	cursor.close();
	assertEquals(realResult, result);
	result = LitePal.where("age > ?", "10").min(studentTable, "age", Integer.TYPE);
	cursor = LitePal.findBySQL("select min(age) from " + studentTable + " where age > ?", "10");
	if (cursor.moveToFirst()) {
		realResult = cursor.getInt(0);
	}
	cursor.close();
	assertEquals(realResult, result);
}
 
Example 5
Source File: QueryMathTest.java    From LitePal with Apache License 2.0 6 votes vote down vote up
@Test
public void testSum() {
	int result = LitePal.sum(Student.class, "age", Integer.TYPE);
	int realResult = -100;
	Cursor cursor = LitePal.findBySQL("select sum(age) from " + studentTable);
	if (cursor.moveToFirst()) {
		realResult = cursor.getInt(0);
	}
	cursor.close();
	assertEquals(realResult, result);
	result = LitePal.where("age > ?", "15").sum(studentTable, "age", Integer.TYPE);
	cursor = LitePal.findBySQL("select sum(age) from " + studentTable + " where age > ?", "15");
	if (cursor.moveToFirst()) {
		realResult = cursor.getInt(0);
	}
	cursor.close();
	assertEquals(realResult, result);
}
 
Example 6
Source File: QueryBySQLTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryBySQLWithPlaceHolder() {
	Cursor cursor = LitePal.findBySQL(
			"select * from " + bookTable + " where id=? and bookname=? and pages=?",
			String.valueOf(book.getId()), "数据库", "300");
	assertTrue(cursor.getCount() == 1);
	cursor.moveToFirst();
	String bookName = cursor.getString(cursor.getColumnIndexOrThrow("bookname"));
	int pages = cursor.getInt(cursor.getColumnIndexOrThrow("pages"));
	assertEquals(bookName, "数据库");
	assertEquals(pages, 300);
	cursor.close();
}
 
Example 7
Source File: QueryBySQLTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryBySQLWithWrongParams() {
	try {
           LitePal.findBySQL("select * from " + bookTable + " where id=? and bookname=? and pages=?",
				String.valueOf(book.getId()), "数据库");
		fail();
	} catch (DataSupportException e) {
		assertEquals("The parameters in conditions are incorrect.", e.getMessage());
	}
	Cursor cursor = LitePal.findBySQL(new String[] {});
	assertNull(cursor);
	cursor = LitePal.findBySQL();
	assertNull(cursor);
}
 
Example 8
Source File: DeleteTest.java    From LitePal with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteWithGenericData() {
    Classroom classroom = new Classroom();
    classroom.setName("classroom1");
    classroom.getNews().add("news1");
    classroom.getNews().add("news2");
    classroom.getNews().add("news3");
    classroom.save();
    int id = classroom.get_id();
    String tableName = DBUtility.getGenericTableName(Classroom.class.getName(), "news");
    String column = DBUtility.getGenericValueIdColumnName(Classroom.class.getName());
    Cursor c = LitePal.findBySQL("select * from " + tableName + " where " + column + " = ?", String.valueOf(id));
    assertEquals(3, c.getCount());
    c.close();
    classroom.delete();
    c = LitePal.findBySQL("select * from " + tableName + " where " + column + " = ?", String.valueOf(id));
    assertEquals(0, c.getCount());
    c.close();
    assertFalse(classroom.isSaved());
    classroom.save();
    assertTrue(classroom.isSaved());
    c = LitePal.findBySQL("select * from " + tableName + " where " + column + " = ?", String.valueOf(classroom.get_id()));
    assertEquals(3, c.getCount());
    c.close();
    LitePal.deleteAll(Classroom.class, "id = ?", String.valueOf(classroom.get_id()));
    c = LitePal.findBySQL("select * from " + tableName + " where " + column + " = ?", String.valueOf(classroom.get_id()));
    assertEquals(0, c.getCount());
    c.close();
}
 
Example 9
Source File: QueryBySQLTest.java    From LitePal with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryBySQL() {
	Cursor cursor = LitePal.findBySQL("select * from " + bookTable);
	assertTrue(cursor.getCount() > 0);
	cursor.close();
}