我在我的Android应用程序中使用DroidDao来获取持久的SQLite数据库。
我有一个连接表:
PART_ID
GPS_ID
SAND_ID
我在这个库中遇到两个函数的问题:
/**Get an Object by clause*/
public T getByClause(String clause, String[] clauseArgs) {
T object = null;
Cursor cursor = database.query(getTableName(), getArrayColumns(),
clause, clauseArgs, null, null, "1");
if(cursor.moveToFirst()){
try {
object = buildDataFromCursor(cursor);
} catch (Exception e) {
e.printStackTrace();
}
}
if(! cursor.isClosed()){
cursor.close();
}
return object;
}
和
/**List items by clause*/
public List<T> getAllbyClause(String clause, String[] clauseArgs, String groupBy, String having, String orderBy) {
List<T> objectList = new ArrayList<T>();
Cursor cursor = database.query(getTableName(), getArrayColumns(),
clause, clauseArgs, groupBy, having, orderBy);
if(cursor.moveToFirst()){
try {
do{
T object = buildDataFromCursor(cursor);
if(object != null){
objectList.add(object);
}
}
while(cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
}
}
if(! cursor.isClosed()){
cursor.close();
}
return objectList;
}
我同样使用它们:
Connection connection = myApp.getDataManager().getConnectionDao().getByClause("PART_ID = ?", new String[]{"44"});
List<Connection> connections = myApp.getDataManager().getConnectionDao().getAllbyClause("PART_ID = ?", new String[]{"44"}, null, null, null);
对于PART_ID,GPS_ID和SAND_ID,这两个函数都返回0。即使我插入一个不正确的ID,函数应该返回NULL,它仍然返回一个零的对象。
有什么想法吗?
答案 0 :(得分:0)
好吧,我通过跟踪代码来弄明白。
我想:buildDataFromCursor(cursor);是一个来自android数据库导入的函数,但它是来自DroidDao.java的方法。我的问题是我的Connection对象中没有SETTERS,其中buildDataFromCursor(cursor);使用setter将数据加载到对象中。
你的setter需要具有以下结构:
Column name:
PART_ID
Setter:
void setPart_id(int value)...