package dbAdapter;
import java.sql.Date;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ProfileDBAdapter {
int sessionId = 0;
private static final String TAG = "DBAdapter";
private static final String KEY_USERID = "userid";
private static final String DATABASE_TABLE1 = "Profile";
private static final String DATABASE_TABLE2 = "DailyInfo";
public static final String CREATE_TABLE1=
"create table if not exists Profile(id integer primary key autoincrement, "
+ "email text not null,"
+ "height_ft text,"
+ "height_inch text,"
+ "weight text,"
+ "age text,"
+ "gender text,"
+ "bmi text,"
+ "max_calorie_consumption text,"
+ "low text,"
+ "med text,"
+ "high text"
+");";
/* public static final String CREATE_TABLE2=
"create table if not exists DailyInfo(id text not null, "
+ "cal_consumed text ,"
+ "cal_burned text,"
+ "day text,"
+ "month text,"
+ "year text"
+");";*/
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public ProfileDBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
// DBHelper = new DatabaseHelper(this);
}
public ProfileDBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
public double round(double value, int decimalPlace)
{
double power_of_ten = 1;
// floating point arithmetic can be very tricky.
double some_factor = 0.05;
while (decimalPlace-- > 0) {
power_of_ten *= 10.0d;
some_factor /= 10.0d;
}
return Math.round((value + some_factor)* power_of_ten) / power_of_ten;
}
public long insertUserInfo(String email, String height_ft, String height_in, String weight, String age,String gender, Float bmi,Double maxCal,Double low,Double med,Double high )
{
long result = 0;
ContentValues initialValues = new ContentValues();
initialValues.put("email", email.toString());
initialValues.put("height_ft", height_ft);
initialValues.put("height_inch", height_in);
initialValues.put("weight", weight);
initialValues.put("age", age);
initialValues.put("gender", gender);
initialValues.put("bmi", Double.toString(round(bmi,1)));
initialValues.put("max_calorie_consumption", Double.toString(round(maxCal,1)));
initialValues.put("low", Double.toString(round(low,1)));
initialValues.put("med", Double.toString(round(med,1)));
initialValues.put("high", Double.toString(round(high,1)));
System.out.println("Inside insert into DB: " + initialValues.get(email));
System.out.println("Inside insert into DB: " + initialValues.get(gender));
System.out.println(email +height_ft +height_in+weight+age+gender +Double.toString(round(bmi,1))+ Double.toString(round(maxCal,1))+Double.toString(round(low,1))+Double.toString(round(med,1))+Double.toString(round(high,1)));
try{
result = db.insert("Profile", null, initialValues);
} catch (Exception ex){
System.out.println("Exception in insert");
}
return result;
}
//Insert DailyInfo
public long insertDailyInfo(String id, String cal_consumed, String catName, String cal_burned, String day, String month, String year )
{
long result = 0;
ContentValues initialValues = new ContentValues();
initialValues.put("id", id);
initialValues.put("cal_consumed", cal_consumed);
initialValues.put("CategoryName",catName);
initialValues.put("cal_burned", cal_burned);
initialValues.put("day", day);
initialValues.put("month", month);
initialValues.put("year", year);
// System.out.println("Inside insert into DB: " + initialValues.get(cal_consumed));
try{
result = db.insert("DailyInfo", null, initialValues);
} catch (Exception ex){
System.out.println("Exception in insert");
}
return result;
}
public int getId(){
System.out.println("inside getID");
Cursor cc = db.rawQuery("select * from Profile", null);
System.out.println("inside getID 1");
if (cc.moveToFirst()) {
do {
System.out.println("inside getID 2");
sessionId = Integer.parseInt(cc.getString(0));
System.out.println("inside getID 3" +sessionId) ;
} while (cc.moveToNext());
}
else
System.out.println("No data found");
cc.close();
return sessionId;
}
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE1, KEY_USERID + "=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllUsers()
{
Cursor cursor = db.rawQuery("select * from Profile", null);
System.out.println("Inside get users from DB");
Log.v("XXXXX", "" + cursor.getCount());
return cursor;
}
}
|