-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBaseHelper.java
More file actions
75 lines (63 loc) · 2.64 KB
/
DataBaseHelper.java
File metadata and controls
75 lines (63 loc) · 2.64 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
package com.example.caltrack;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "details.db";
public static final String TABLE_NAME = "details";
public static final String COL1 = "NAME";
public static final String COL2 = "PROFESSION";
public static final String COL3 = "WEIGHT";
public static final String COL4 = "BMI";
public static final String COL5 = "AGE";
public static final String COL6 = "HEIGHT";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(NAME TEXT, PROFESSION TEXT, WEIGHT INT, BMI REAL, AGE INT, HEIGHT INT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void deleteProduct(String name) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + COL1 + "='" + name + "';");
}
public boolean addData(String name, String prof, int weight, double bmi,int age, int height) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1, name);
contentValues.put(COL2, prof);
contentValues.put(COL3, weight);
contentValues.put(COL4, bmi);
contentValues.put(COL5, age);
contentValues.put(COL6, height);
long result = db.insert(TABLE_NAME, null, contentValues);
Log.d("MI", result + "");
//if data as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
//query for outputting the whole table
public Cursor getListContents() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
public Cursor getHealthyBmiContents() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME + " a WHERE a.BMI >= 18.5 AND a.BMI <= 24.9", null);
return data;
}
}