Guest User

Untitled

a guest
Aug 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. public void insertCaloriesCountIntoDB(String date,int caloriesCount) {
  2. SQLiteDatabase db = this.getWritableDatabase();
  3. //проверяю есть ли запись с таким именем в таблице
  4. String query ="SELECT * FROM TABLENAME WHERE date = "+date;
  5. Cursor cursor =db.rawQuery(query,null);
  6. if(cursor.getCount()==0){
  7. //если нету записываю
  8. String insert ="INSERT INTO TABLENAME (date,calcount) VALUES (" + date + ","+ caloriesCount + ")";
  9. db.execSQL(insert);
  10. cursor.close();
  11. db.close();
  12.  
  13. }else{
  14. //если есть обновляю
  15. String update ="UPDATE TABLENAME SET calcount = calcount + "+caloriesCount+" WHERE date = "+date;
  16. db.execSQL(update);
  17. cursor.close();
  18. db.close();
  19.  
  20. }
  21.  
  22. public int getCaloriesCount(String date){
  23. SQLiteDatabase db = this.getWritableDatabase();
  24. Cursor cursor=db.rawQuery("SELECT calcount FROM TABLENAME where date = ?", new String[]{date});
  25. while (cursor.moveToNext()) {
  26. calCount = cursor.getInt(0);
  27. }
  28. return calCount;
  29. }
Add Comment
Please, Sign In to add comment