Advertisement
Guest User

Untitled

a guest
May 27th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. package com.none.myapplication;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.DatabaseUtils;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. public class DatabaseHandler extends SQLiteOpenHelper {
  15.  
  16. // Database Version
  17. private static final int DATABASE_VERSION = 1;
  18.  
  19. // Database Name
  20. private static final String DATABASE_NAME = "database.db";
  21.  
  22. //table name
  23. private static final String TABLE_DETAILS = "details";
  24. private static final String TABLE_FOOD = "food";
  25. private static final String TABLE_OLDDETAILS = "oldDetails";
  26. private static final String TABLE_SETTING = "setting";
  27.  
  28. //Table Columns names
  29. private static final String KEY_ID = "id";
  30. private static final String KEY_NAME = "name";
  31. private static final String KEY_HEIGHT = "height";
  32. private static final String KEY_WEIGHT = "weight";
  33. private static final String KEY_CALORIES = "calories";
  34. private static final String KEY_DATE = "date";
  35. private static final String KEY_LEVEL = "level";
  36. private static final String KEY_DURATION = "duration";
  37. private static final String KEY_DAYS = "days";
  38.  
  39.  
  40. public DatabaseHandler(Context context) {
  41. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  42. }
  43.  
  44. // Creating Tables
  45. @Override
  46. public void onCreate(SQLiteDatabase db) {
  47. String CREATE_DETAILS_TABLE = "CREATE TABLE " + TABLE_DETAILS + "("
  48. + KEY_ID + " INTEGER PRIMARY KEY," + KEY_HEIGHT + " REAL," + KEY_WEIGHT + " REAL " + ")";
  49. String CREATE_FOOD_TABLE = "CREATE TABLE " + TABLE_FOOD + "("
  50. + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_CALORIES + " INTEGER " + ")";
  51. String CREATE_OLDDETAILS_TABLE = "CREATE TABLE " + TABLE_OLDDETAILS + "("
  52. + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT," + KEY_HEIGHT + " REAL," + KEY_WEIGHT + " REAL " + ")";
  53. String CREATE_SETTING_TABLE = "CREATE TABLE " + TABLE_SETTING + "("
  54. + KEY_ID + " INTEGER PRIMARY KEY," + KEY_LEVEL + " INTEGER," + KEY_DURATION + " INTEGER," + KEY_DAYS + " INTEGER " + ")";
  55.  
  56. db.execSQL(CREATE_OLDDETAILS_TABLE);
  57. db.execSQL(CREATE_DETAILS_TABLE);
  58. db.execSQL(CREATE_FOOD_TABLE);
  59. db.execSQL(CREATE_SETTING_TABLE);
  60. }
  61.  
  62. // Upgrading database
  63. @Override
  64. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  65. // Drop older table if existed
  66. db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
  67. db.execSQL("DROP TABLE IF EXISTS " + TABLE_OLDDETAILS);
  68. db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOD);
  69. db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETTING);
  70.  
  71. // Create tables again
  72. onCreate(db);
  73. }
  74.  
  75. /**
  76. * All CRUD(Create, Read, Update, Delete) Operations
  77. */
  78.  
  79. // Adding new data
  80. public boolean addDetails(double height, double weight) {
  81. SQLiteDatabase db = this.getWritableDatabase();
  82.  
  83. ContentValues values = new ContentValues();
  84. values.put(KEY_ID, 1);
  85. values.put(KEY_HEIGHT, height);
  86. values.put(KEY_WEIGHT, weight);
  87.  
  88. // Inserting Row
  89. long result = db.insert(TABLE_DETAILS, null, values);
  90. if(result == -1){
  91. return false;
  92. }
  93. else{
  94. return true;
  95. }
  96. }
  97. public boolean addOldDetails(String date, double height, double weight) {
  98. SQLiteDatabase db = this.getWritableDatabase();
  99.  
  100. ContentValues values = new ContentValues();
  101. values.put(KEY_DATE, date);
  102. values.put(KEY_HEIGHT, height);
  103. values.put(KEY_WEIGHT, weight);
  104.  
  105. // Inserting Row
  106. long result = db.insert(TABLE_OLDDETAILS, null, values);
  107. if(result == -1){
  108. return false;
  109. }
  110. else{
  111. return true;
  112. }
  113. }
  114. public boolean addFood(String name, int calories) {
  115. SQLiteDatabase db = this.getWritableDatabase();
  116.  
  117. ContentValues values = new ContentValues();
  118. values.put(KEY_NAME, name);
  119. values.put(KEY_CALORIES, calories);
  120.  
  121. // Inserting Row
  122. long result = db.insert(TABLE_FOOD, null, values);
  123. if(result == -1){
  124. return false;
  125. }
  126. else{
  127. return true;
  128. }
  129. }
  130. public boolean addSetting(int level, int duration, int days) {
  131. SQLiteDatabase db = this.getWritableDatabase();
  132.  
  133. ContentValues values = new ContentValues();
  134. values.put(KEY_ID, 1);
  135. values.put(KEY_LEVEL, level);
  136. values.put(KEY_DURATION, duration);
  137. values.put(KEY_DAYS, days);
  138.  
  139. // Inserting Row
  140. long result = db.insert(TABLE_SETTING, null, values);
  141. if(result == -1){
  142. return false;
  143. }
  144. else{
  145. return true;
  146. }
  147. }
  148. //Checks if TABLE DETAILS is empty
  149. public boolean checkDetails(){
  150. SQLiteDatabase db = this.getWritableDatabase();
  151. String selectQuery = "SELECT * FROM " + TABLE_DETAILS;
  152. Cursor cursor = db.rawQuery(selectQuery, null);
  153. Boolean rowExists;
  154.  
  155. if (cursor.moveToFirst())
  156. {
  157. // DO SOMETHING WITH CURSOR
  158. rowExists = true;
  159.  
  160. } else
  161. {
  162. // I AM EMPTY
  163. rowExists = false;
  164. }
  165. return rowExists;
  166. }
  167.  
  168. //Checks if TABLE OLDDETAILS is empty
  169. public boolean checkOldDetails(){
  170. SQLiteDatabase db = this.getWritableDatabase();
  171. String selectQuery = "SELECT * FROM " + TABLE_OLDDETAILS;
  172. Cursor cursor = db.rawQuery(selectQuery, null);
  173. Boolean rowExists;
  174.  
  175. if (cursor.moveToFirst())
  176. {
  177. // DO SOMETHING WITH CURSOR
  178. rowExists = true;
  179.  
  180. } else
  181. {
  182. // I AM EMPTY
  183. rowExists = false;
  184. }
  185. return rowExists;
  186. }
  187.  
  188. //Checks if TABLE FOOD is empty
  189. public boolean checkFood(){
  190. SQLiteDatabase db = this.getWritableDatabase();
  191. String selectQuery = "SELECT * FROM " + TABLE_FOOD;
  192. Cursor cursor = db.rawQuery(selectQuery, null);
  193. Boolean rowExists;
  194.  
  195. if (cursor.moveToFirst())
  196. {
  197. // DO SOMETHING WITH CURSOR
  198. rowExists = true;
  199.  
  200. } else
  201. {
  202. // I AM EMPTY
  203. rowExists = false;
  204. }
  205. return rowExists;
  206. }
  207. public boolean checkSetting(){
  208. SQLiteDatabase db = this.getWritableDatabase();
  209. String selectQuery = "SELECT * FROM " + TABLE_SETTING;
  210. Cursor cursor = db.rawQuery(selectQuery, null);
  211. Boolean rowExists;
  212.  
  213. if (cursor.moveToFirst())
  214. {
  215. // DO SOMETHING WITH CURSOR
  216. rowExists = true;
  217.  
  218. } else
  219. {
  220. // I AM EMPTY
  221. rowExists = false;
  222. }
  223. return rowExists;
  224. }
  225. // Getting All Details
  226. public Details getDetails() {
  227. String selectQuery = "SELECT * FROM " + TABLE_DETAILS;
  228. SQLiteDatabase db = this.getWritableDatabase();
  229. Cursor cursor = db.rawQuery(selectQuery, null);
  230. if (cursor != null)
  231. cursor.moveToFirst();
  232. Details detail = new Details(cursor.getDouble(1), cursor.getDouble(2));
  233.  
  234. return detail;
  235. }
  236.  
  237. // Getting all OldDetails
  238. public List<oldDetails> getOldDetails() {
  239. List<oldDetails> detailsList = new ArrayList<oldDetails>();
  240. // Select All Query
  241. String selectQuery = "SELECT * FROM " + TABLE_OLDDETAILS;
  242.  
  243. SQLiteDatabase db = this.getWritableDatabase();
  244. Cursor cursor = db.rawQuery(selectQuery, null);
  245.  
  246. // looping through all rows and adding to list
  247. if (cursor.moveToFirst()) {
  248. do {
  249. oldDetails details = new oldDetails();
  250. details.setId(Integer.parseInt(cursor.getString(0)));
  251. details.setDate(cursor.getString(1));
  252. details.setHeight(cursor.getDouble(2));
  253. details.setWeight(cursor.getDouble(3));
  254. // Adding to list
  255. detailsList.add(details);
  256. } while (cursor.moveToNext());
  257. }
  258.  
  259. // return list
  260. return detailsList;
  261. }
  262. public long QueryNumEntries()
  263. {
  264. SQLiteDatabase db = this.getReadableDatabase();
  265. return DatabaseUtils.queryNumEntries(db, TABLE_OLDDETAILS);
  266. }
  267.  
  268. public setting getSetting() {
  269. String selectQuery = "SELECT * FROM " + TABLE_SETTING;
  270. SQLiteDatabase db = this.getWritableDatabase();
  271. Cursor cursor = db.rawQuery(selectQuery, null);
  272. if (cursor != null)
  273. cursor.moveToFirst();
  274. setting set = new setting(cursor.getInt(1), cursor.getInt(2), cursor.getInt(3));
  275.  
  276. return set;
  277. }
  278.  
  279. // Getting All Food
  280. public List<cFood> getFood() {
  281. List<cFood> foodList = new ArrayList<cFood>();
  282. // Select All Query
  283. String selectQuery = "SELECT * FROM " + TABLE_FOOD;
  284.  
  285. SQLiteDatabase db = this.getWritableDatabase();
  286. Cursor cursor = db.rawQuery(selectQuery, null);
  287.  
  288. // looping through all rows and adding to list
  289. if (cursor.moveToFirst()) {
  290. do {
  291. cFood food = new cFood();
  292. food.setId(Integer.parseInt(cursor.getString(0)));
  293. food.setName(cursor.getString(1));
  294. food.setCalories(cursor.getInt(2));
  295. // Adding to list
  296. foodList.add(food);
  297. } while (cursor.moveToNext());
  298. }
  299.  
  300. // return list
  301. return foodList;
  302. }
  303.  
  304. public int updateDetails(Details detail) {
  305. SQLiteDatabase db = this.getWritableDatabase();
  306. ContentValues values = new ContentValues();
  307. values.put(KEY_HEIGHT, detail.getHeight());
  308. values.put(KEY_WEIGHT, detail.getWeight());
  309. Log.d("UPDATE: ", "updated all");
  310.  
  311. // updating row
  312. return db.update(TABLE_DETAILS, values, KEY_ID + " = ?", new String[] { String.valueOf(1) });
  313. }
  314.  
  315. public int updateSetting(setting set) {
  316. SQLiteDatabase db = this.getWritableDatabase();
  317. ContentValues values = new ContentValues();
  318. values.put(KEY_LEVEL, set.getLevel());
  319. values.put(KEY_DURATION, set.getDuration());
  320. values.put(KEY_DAYS, set.getDays());
  321. Log.d("UPDATE: ", "updated all");
  322.  
  323. // updating row
  324. return db.update(TABLE_SETTING, values, KEY_ID + " = ?", new String[] { String.valueOf(1) });
  325. }
  326.  
  327.  
  328. public void deleteFood(cFood food) {
  329. SQLiteDatabase db = this.getWritableDatabase();
  330. db.delete(TABLE_FOOD, KEY_ID + " = ?",
  331. new String[] { String.valueOf(food.getId()) });
  332. db.close();
  333. }
  334. public void deleteAllFood(){
  335. SQLiteDatabase db = this.getWritableDatabase();
  336. db.execSQL("DELETE FROM " + TABLE_FOOD);
  337. db.close();
  338. }
  339. public long QueryNumEntries1()
  340. {
  341. SQLiteDatabase db = this.getReadableDatabase();
  342. return DatabaseUtils.queryNumEntries(db, TABLE_FOOD);
  343. }
  344. /*
  345. // Deleting single contact
  346. public void deleteContact(Contact contact) {
  347. SQLiteDatabase db = this.getWritableDatabase();
  348. db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
  349. new String[] { String.valueOf(contact.getID()) });
  350. db.close();
  351. }
  352.  
  353.  
  354. */
  355. }
  356.  
  357.  
  358.  
  359. ERROR Received:
  360.  
  361. E/AndroidRuntime: FATAL EXCEPTION: main
  362. Process: com.none.myapplication, PID: 5230
  363. android.database.sqlite.SQLiteException: no such table: setting (code 1): , while compiling: SELECT * FROM setting
  364. at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  365. at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
  366. at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
  367. at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  368. at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  369. at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
  370. at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
  371. at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
  372. at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
  373. at com.none.myapplication.DatabaseHandler.checkSetting(DatabaseHandler.java:210)
  374. at com.none.myapplication.PageFragment$4.onClick(PageFragment.java:107)
  375. at android.view.View.performClick(View.java:5200)
  376. at android.view.View$PerformClick.run(View.java:21163)
  377. at android.os.Handler.handleCallback(Handler.java:739)
  378. at android.os.Handler.dispatchMessage(Handler.java:95)
  379. at android.os.Looper.loop(Looper.java:148)
  380. at android.app.ActivityThread.main(ActivityThread.java:5436)
  381. at java.lang.reflect.Method.invoke(Native Method)
  382. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
  383. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement