DbBase.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.topsoft.jianyu.SQLite;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.util.Log;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10. import java.util.List;
  11. import static android.content.Context.MODE_PRIVATE;
  12. /**
  13. * Created by wangkaiyue on 2018/1/26.
  14. */
  15. public class DbBase {
  16. private final String DB_NAME="jy_db";
  17. private final String MASSAHE_TABLE_NAME="jy_message";
  18. static SharedPreferences share=null;
  19. private static DbBase dbBase;
  20. private static Context context;
  21. private final String LOG_TAG = "DbBase";
  22. public DbBase(Context context){
  23. this.context=context;
  24. }
  25. public static DbBase getInstance(Context ctx){
  26. context=ctx;
  27. if (dbBase==null){
  28. dbBase=new DbBase(ctx);
  29. }
  30. share=context.getSharedPreferences("jy", MODE_PRIVATE);
  31. return dbBase;
  32. }
  33. //插入一条信息
  34. public boolean insertMessage(JyMessage message){
  35. boolean result = true;
  36. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  37. SQLiteDatabase db =dbHelper.getWritableDatabase();
  38. try{
  39. db.beginTransaction();
  40. ContentValues cv= message.getMsaagesContentValues();
  41. if(db.insert(MASSAHE_TABLE_NAME, null, cv)<0){
  42. result=false;
  43. }
  44. if (result) {
  45. db.setTransactionSuccessful();
  46. }
  47. }catch (Exception e) {
  48. Log.e(LOG_TAG,e.getMessage());
  49. return false;
  50. } finally {
  51. try {
  52. if (null != db) {
  53. db.endTransaction();
  54. db.close();
  55. }
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. return result;
  61. }
  62. //取单条通知消息
  63. public JSONObject findOneMessage(String id){
  64. JSONObject msg=new JSONObject();
  65. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  66. SQLiteDatabase db =dbHelper.getReadableDatabase();
  67. try{
  68. Cursor cursor = db.query(MASSAHE_TABLE_NAME, new String[]{"id","title","content","link","category","openid","readed","comeintime","descript","extend"}, "id=?", new String[]{id}, null, null, null);
  69. while(cursor.moveToNext()){
  70. msg.put("id",cursor.getInt(cursor.getColumnIndex("id"))+"");
  71. msg.put("title",cursor.getString(cursor.getColumnIndex("title")));
  72. msg.put("content",cursor.getString(cursor.getColumnIndex("content")));
  73. msg.put("link",cursor.getString(cursor.getColumnIndex("link")));
  74. msg.put("category",cursor.getString(cursor.getColumnIndex("category")));
  75. msg.put("readed",cursor.getInt(cursor.getColumnIndex("readed"))+"");
  76. msg.put("comeintime",cursor.getInt(cursor.getColumnIndex("comeintime"))+"");
  77. msg.put("openid",cursor.getString(cursor.getColumnIndex("openid")));
  78. msg.put("descript",cursor.getString(cursor.getColumnIndex("descript")));
  79. msg.put("extend",cursor.getString(cursor.getColumnIndex("extend")));
  80. }
  81. }catch (Exception e){
  82. Log.e(LOG_TAG,e.getMessage());
  83. return null;
  84. }finally {
  85. if (null != db) {
  86. db.close();
  87. }
  88. }
  89. return msg;
  90. }
  91. //更新消息状态
  92. public boolean updateMessageState(Integer id,boolean state) {
  93. boolean result = true;
  94. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  95. SQLiteDatabase db =dbHelper.getWritableDatabase();
  96. try {
  97. db.beginTransaction();
  98. ContentValues cv = new ContentValues();
  99. if(state){
  100. cv.put("readed", 1);
  101. }else{
  102. cv.put("readed", 0);
  103. }
  104. String whereClause = "id=?";
  105. String[] whereArgs = {id.toString()};
  106. //参数1 是要更新的表名
  107. //参数2 是一个ContentValeus对象
  108. //参数3 是where子句
  109. if(db.update(MASSAHE_TABLE_NAME, cv, whereClause, whereArgs)<0){
  110. result=false;
  111. }
  112. if (result) {
  113. db.setTransactionSuccessful();
  114. }
  115. } catch (Exception e) {
  116. Log.e(LOG_TAG,e.getMessage());
  117. return false;
  118. } finally {
  119. try {
  120. if (null != db) {
  121. db.endTransaction();
  122. db.close();
  123. }
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. return result;
  129. }
  130. //根据消息的推送时间改变为已读信息
  131. public boolean updateMessageState(String pushTime) {
  132. if("".equals(pushTime)){
  133. return false;
  134. }
  135. boolean result = true;
  136. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  137. SQLiteDatabase db =dbHelper.getWritableDatabase();
  138. try {
  139. db.beginTransaction();
  140. ContentValues cv = new ContentValues();
  141. cv.put("readed", 1);
  142. String whereClause = "pushTime=?";
  143. String[] whereArgs = {pushTime};
  144. //参数1 是要更新的表名
  145. //参数2 是一个ContentValeus对象
  146. //参数3 是where子句
  147. if(db.update(MASSAHE_TABLE_NAME, cv, whereClause, whereArgs)<0){
  148. result=false;
  149. }
  150. if (result) {
  151. db.setTransactionSuccessful();
  152. }
  153. } catch (Exception e) {
  154. Log.e(LOG_TAG,e.getMessage());
  155. return false;
  156. } finally {
  157. try {
  158. if (null != db) {
  159. db.endTransaction();
  160. db.close();
  161. }
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. return result;
  167. }
  168. //根据推送时间获取信息id
  169. public Integer getIDbypushTime(String pushTime){
  170. Integer id=-1;
  171. if("".equals(pushTime)){
  172. return id;
  173. }
  174. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  175. SQLiteDatabase db =dbHelper.getReadableDatabase();
  176. try{
  177. Cursor cursor=db.query(MASSAHE_TABLE_NAME, new String[]{"id"}, "pushTime=?", new String[]{pushTime}, null, null, null);
  178. if(cursor.moveToNext()){
  179. id=cursor.getInt(cursor.getColumnIndex("id"));
  180. }
  181. return id;
  182. }catch (Exception e){
  183. e.printStackTrace();
  184. }finally {
  185. if (null != db) {
  186. db.close();
  187. }
  188. }
  189. return id;
  190. }
  191. //删除一条信息
  192. public boolean deleteMessageById(Integer id){
  193. boolean result = true;
  194. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  195. SQLiteDatabase db =dbHelper.getReadableDatabase();
  196. try {
  197. db.beginTransaction();
  198. String whereClauses = "id=?";
  199. String [] whereArgs = {id.toString()};
  200. if(db.delete(MASSAHE_TABLE_NAME, whereClauses, whereArgs)<0){
  201. result = false;
  202. }
  203. if (result) {
  204. db.setTransactionSuccessful();
  205. }
  206. } catch (Exception e) {
  207. Log.e(LOG_TAG,e.getMessage());
  208. return false;
  209. } finally {
  210. try {
  211. if (null != db) {
  212. db.endTransaction();
  213. db.close();
  214. }
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. return result;
  220. }
  221. //批量插入
  222. public boolean insertMessageList(List<JyMessage> list){
  223. boolean result = true;
  224. if (null == list || list.size() <= 0) {
  225. return true;
  226. }
  227. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  228. SQLiteDatabase db =dbHelper.getWritableDatabase();
  229. try {
  230. String openid = share.getString("openid", "");
  231. if("".equals(openid)){
  232. return false; //openid为空
  233. }
  234. db.beginTransaction();
  235. for (JyMessage msg : list) {
  236. ContentValues values = msg.getMsaagesContentValues();
  237. values.put("openid",openid);
  238. if (db.insert(MASSAHE_TABLE_NAME, null, values) < 0) {
  239. result = false;
  240. break;
  241. }
  242. }
  243. if (result) {
  244. db.setTransactionSuccessful();
  245. }
  246. } catch (Exception e) {
  247. Log.e(LOG_TAG,e.getMessage());
  248. return false;
  249. } finally {
  250. try {
  251. if (null != db) {
  252. db.endTransaction();
  253. db.close();
  254. }
  255. } catch (Exception e) {
  256. e.printStackTrace();
  257. }
  258. }
  259. return result;
  260. }
  261. //按通知接收时间倒排,取前100条(或者做分页处理)。
  262. public JSONArray getMessageList(String openid){
  263. JSONArray list=new JSONArray();
  264. JSONObject msg=null;
  265. DBHelper dbHelper = new DBHelper(context,"jy_db",null,1);
  266. SQLiteDatabase db =dbHelper.getWritableDatabase();
  267. try{
  268. Cursor cursor=null;
  269. if("".equals(openid)){
  270. cursor = db.query("jy_message", new String[]{"id","title","content","link","category","openid","readed","comeintime","descript","extend"}, "openid=?", new String[]{""}, null, null, "comeintime desc");
  271. }else{
  272. cursor = db.query("jy_message", new String[]{"id","title","content","link","category","openid","readed","comeintime","descript","extend"}, "openid=? or openid=?", new String[]{openid,""}, null, null, "comeintime desc");
  273. }
  274. int count=0;
  275. while(cursor.moveToNext()&&count<100){
  276. count++;
  277. msg=new JSONObject();
  278. msg.put("id",cursor.getInt(cursor.getColumnIndex("id"))+"");
  279. msg.put("title",cursor.getString(cursor.getColumnIndex("title")));
  280. msg.put("content",cursor.getString(cursor.getColumnIndex("content")));
  281. msg.put("link",cursor.getString(cursor.getColumnIndex("link")));
  282. msg.put("category",cursor.getString(cursor.getColumnIndex("category")));
  283. msg.put("openid",cursor.getString(cursor.getColumnIndex("openid")));
  284. msg.put("readed",cursor.getInt(cursor.getColumnIndex("readed"))+"");
  285. msg.put("comeintime",cursor.getInt(cursor.getColumnIndex("comeintime"))+"");
  286. msg.put("descript",cursor.getString(cursor.getColumnIndex("descript")));
  287. msg.put("extend",cursor.getString(cursor.getColumnIndex("extend")));
  288. list.put(msg);
  289. }
  290. return list;
  291. }catch (Exception e){
  292. Log.e(LOG_TAG,e.getMessage());
  293. }finally{
  294. try {
  295. if (null != db) {
  296. db.close();
  297. }
  298. } catch (Exception e) {
  299. e.printStackTrace();
  300. }
  301. }
  302. return list;
  303. }
  304. //获取未读消息数量
  305. public int getUnReadCount(String openid){
  306. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  307. SQLiteDatabase db =dbHelper.getReadableDatabase();
  308. try{
  309. //参数1:表名
  310. //参数2:要想显示的列
  311. //参数3:where子句
  312. //参数4:where子句对应的条件值
  313. //参数5:分组方式
  314. //参数6:having条件
  315. //参数7:排序方式
  316. Cursor cursor =null;
  317. if("".equals(openid)){
  318. cursor=db.query(MASSAHE_TABLE_NAME, new String[]{"id","title","content","link","category","openid","readed","comeintime","descript","extend"}, "openid=? and readed=?", new String[]{"","0"}, null, null, null);
  319. }else{
  320. cursor=db.query(MASSAHE_TABLE_NAME, new String[]{"id","title","content","link","category","openid","readed","comeintime","descript","extend"}, "(openid=? or openid=?)and readed=?", new String[]{openid,"","0"}, null, null, null);
  321. }
  322. return cursor.getCount();
  323. }catch (Exception e){
  324. e.printStackTrace();
  325. }finally {
  326. if (null != db) {
  327. db.close();
  328. }
  329. }
  330. return 0;
  331. }
  332. //所有信息设置为已读
  333. public int updateAllMegReaded(String openid){
  334. int num=0;
  335. DBHelper dbHelper = new DBHelper(context,DB_NAME,null,1);
  336. SQLiteDatabase db =dbHelper.getWritableDatabase();
  337. try {
  338. db.beginTransaction();
  339. ContentValues cv = new ContentValues();
  340. cv.put("readed", 1);
  341. if("".equals(openid)){
  342. num=db.update(MASSAHE_TABLE_NAME, cv, "openid=?", new String[]{""});
  343. }else{
  344. num=db.update(MASSAHE_TABLE_NAME, cv, "openid=? or openid=?", new String[]{openid,""});
  345. }
  346. if (num>0) {
  347. db.setTransactionSuccessful();
  348. }
  349. } catch (Exception e) {
  350. Log.e(LOG_TAG,e.getMessage());
  351. return num;
  352. } finally {
  353. try {
  354. if (null != db) {
  355. db.endTransaction();
  356. db.close();
  357. }
  358. } catch (Exception e) {
  359. e.printStackTrace();
  360. }
  361. }
  362. return num;
  363. }
  364. }