ChildInfoDao.java
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.shunzhi.parent.db;
import android.content.Context;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.shunzhi.parent.bean.ChildBean;
import com.shunzhi.parent.util.DatabaseHelper;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/4/19 0019.
*/
public class ChildInfoDao {
private Context context;
private DatabaseHelper databaseHelper;
private Dao<ChildBean, Integer> dao;
public ChildInfoDao(Context context) {
this.context = context;
try {
databaseHelper = DatabaseHelper.getHelper(context);
dao = databaseHelper.getDao(ChildBean.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean createOrUpdate(ChildBean childBean){
try {
return dao.createOrUpdate(childBean).getNumLinesChanged() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean onCreate(ChildBean childBean) {
try {
return dao.create(childBean) > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean deleteAll(){
try {
DeleteBuilder builder = dao.deleteBuilder();
return dao.delete(builder.prepare())>0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public synchronized List<ChildBean> getAllChildren() {
List<ChildBean> children = null;
try {
children = dao.queryForAll();
if (children != null && children.size() > 0) return children;
} catch (SQLException e) {
e.printStackTrace();
return new ArrayList<>();
}
return new ArrayList<>();
}
public synchronized ChildBean getChildBystudentId(int studentId) {
QueryBuilder queryBuilder = dao.queryBuilder();
try {
queryBuilder.where().eq("studentId", studentId);
List<ChildBean> list = dao.query(queryBuilder.prepare());
ChildBean studentInfo = list.get(0);
return studentInfo;
// return dao.queryForFirst(queryBuilder.prepare());
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}