I'm working on an SQLite based app. Everything is working fine, except my if-else statements in my method. The saving and stuff works, just the checking is giving me a pretty high blood pressure. I'm hoping one of you is much smarter than i am and finds the probably obvious mistake i made:
public void save() {
// get length of EditText
int dateLength, mileageLength, amountLength, lpriceLength, tpriceLength;
dateLength = date_widget.getText().length();
mileageLength = mileage_widget.getText().length();
amountLength = amount_widget.getText().length();
lpriceLength = price_widget.getText().length();
tpriceLength = totalPrice_widget.getText().length();
// Start save method if EditTexts are not empty.
if (dateLength > 0 || mileageLength > 0 || amountLength > 0
|| lpriceLength > 0 || tpriceLength > 0) {
// Get the value of each EditText and write it into the
// String/doubles
String date = date_widget.getText().toString();
double mileage = Double
.valueOf(mileage_widget.getText().toString());
double amount = Double.valueOf(amount_widget.getText().toString());
double lprice = Double.valueOf(price_widget.getText().toString());
double tprice = Double.valueOf(totalPrice_widget.getText()
.toString());
// Check if mileage is increasing, else cancel and show toast
int checkMileage = Integer.parseInt(db
.getSearchResult("mileage", 0));
if (checkMileage < mileage) {
try {
// if (id == null) {
db.insert(date, mileage, amount, lprice, tprice);
Toast.makeText(this, R.string.action_input_saved,
Toast.LENGTH_SHORT).show();
finish();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "ERROR " + e, Toast.LENGTH_LONG)
.show();
}
} else {
Toast.makeText(
this,
"Your current mileage must be more than the last saved mileage",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "finish your input", Toast.LENGTH_LONG).show();
}
}
My Method in the DbAdapter class:
public String getSearchResult(String sql, int cmd) {
if (cmd == 0) {
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
cursor.moveToFirst();
String tmp = cursor.getString(0);
cursor.close();
// return count
return tmp;
} else if (cmd == 1) {
int sum = 0;
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
String idQuery = "SELECT _id FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
Cursor id = db.rawQuery(idQuery, null);
// berechnung
cursor.moveToFirst();
id.moveToFirst();
int maxId = Integer.parseInt(id.getString(0));
for (int i = 0; i < maxId; i++) {
int tmp = Integer.parseInt(cursor.getString(0));
sum = sum + tmp;
cursor.moveToNext();
}
cursor.close();
id.close();
return String.valueOf(sum);
} else if (cmd == 2 && sql == "mileage") {
int sum = 0;
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
String idQuery = "SELECT _id FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
Cursor id = db.rawQuery(idQuery, null);
// berechnung
cursor.moveToFirst();
id.moveToFirst();
int maxId = Integer.parseInt(id.getString(0));
if (maxId > 1) {
int array[] = new int[maxId];
// Array füllen
for (int i = 0; i < maxId; i++) {
array[i] = Integer.parseInt(cursor.getString(0));
// sum = sum + tmp;
cursor.moveToNext();
}
for (int k = 1; k < maxId; k++) {
int tmp;
tmp = array[k] - array[k - 1];
sum = sum + tmp;
}
cursor.close();
id.close();
return String.valueOf(sum);
} else {
return "--";
}
}
return "Wrong CMD";
}
I is pretty messy, i know
Turning comment into an answer:
Switch all || to && in your first if. Otherwise you will try to process everything even if only one field is filled in.
Related
getting values and trying to add to json array
public JSONArray getChooseContact(Context context ,String userid,String bookid , String chapterid, String questionid ) {
DBHelper dbh = new DBHelper(context , LektzDB.DB_NAME, null,
LektzDB.DB_VERSION);
SQLiteDatabase db = dbh.getReadableDatabase();
JSONArray resultSet = new JSONArray();
try{
Cursor cursor = db.rawQuery("SELECT * FROM "
+ TB_AssessmentChooseValues.NAME + " where "
+ TB_AssessmentChooseValues.CL_1_USER_ID + "='"+ userid +"' AND "
+ TB_AssessmentChooseValues.CL_2_BOOK_ID + "='"+ bookid +"' AND "
+ TB_AssessmentChooseValues.CL_3_CHAPTER_ID + "='" + chapterid +"' AND "
+ TB_AssessmentChooseValues.CL_4_QUESTION_ID + "='" + questionid + "'",null);
Log.i("logchoose", "gettingchooseSuccessful");
if(cursor.getCount() > 0) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for (int i = 0; i < totalColumn; i++) {
if (cursor.getColumnName(i) != null) {
try {
rowObject.put(cursor.getColumnName(i),
cursor.getString(i));
} catch (Exception e) {
Log.d("TAG", e.getMessage());
}
}
}
resultSet.put(rowObject);
cursor.moveToNext();
}
cursor.close();
}
}
catch(Exception e)
{
}
return resultSet;
}
and trying to retrieve values as
JSONArray RetrievedChoose = rdb.getChooseContact(getContext(),userid, BookId ,chapter_idchoose ,question_idchoose);
finalassessment.add(String.valueOf(RetrievedChoose));
Log.d("logchoose", "getsuccess"+finalassessment)
The cursor will get two rows of output but in json array im able to get only one value at a time.. I need this operation multiple times in my project please suggest me how to set that json array is not to be replaced for every time
The result is showing two seperate values which firstone is replacing second one
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for (int i = 0; i < totalColumn; i++) {
if (cursor.getColumnName(i) != null) {
try {
rowObject.put(cursor.getColumnName(i),
cursor.getString(i));
} catch (Exception e) {
Log.d("TAG", e.getMessage());
}
}
}
resultSet.put(rowObject);
}
cursor.close();
}
Use above code and return resultSet
I was using the exact same query yesterday and it was working fine today I made a few changes to flow of the program and the query no longer returns and rows.
the first function that my programs goes to:
public void prepareSummary(Date startDate , Date endDate)
{
int getStartDay = getDayFromDate(startDate);
int getStartMonth = getMonthFromDate(startDate);
//
int getEndDay = getDayFromDate(endDate);
int getEndMonth = getMonthFromDate(endDate);
int getYear = getYearFromDate(startDate);
if(getStartMonth <= getEndMonth)
{
if(getStartMonth == getEndMonth)
{
if(getStartDay < getEndDay)
{
while(getStartDay <= getEndDay)
{
Calendar cal = Calendar.getInstance();
cal.set( getYear, getStartMonth, getStartDay);
Date queryStart = getStartOfDay(cal.getTime());
Date queryEnd = getEndOfDay(cal.getTime());
List<Object[]> res = getSumList(queryStart, queryEnd);
doQuery(res);
++getStartDay;
}
}
else
{
}
}
else
{
}
}
else
{
}
}
Here is what getSumList looks like:
public List<Object[]> getSumList(Date start, Date end) {
String query = "";
query += "SELECT COUNT(s) pCount,"
+ "p.nameText,"
+ "g.nameText,"
+ "t.shiftID"
+ " FROM Sheets s , GradeNames g , SpecieNames p, ShiftTimes t"
+ " WHERE s.createdLocal > :start and s.createdLocal < :end"
+ " AND s.specieNameIndex = p.nameIndex "
+ " AND s.gradeNameIndex = g.nameIndex"
+ " AND s.shiftIndex = t.shiftIndex"
+ " GROUP BY p.nameText , g.nameText , t.shiftID";
Query q = em.createQuery(query);
q.setParameter("start", start);
q.setParameter("end", end);
return q.getResultList();
}
This next function doesn't matter at this point because nothing is being executed because the list length is zero:
private void doQuery(List<Object[]> obj)
{
int length = obj.size();
String grade = null;
Long standingCount = (long) 0;
System.out.println("Length" + length);
for (int i = 0; i < length; ++i) {
// HAVE A LIST OF ALL ITEMS PULLED FROM DATABASE
Object[] tmpObj = obj.get(i);
Long tmpCount = (Long) tmpObj[0];
String tmpSpecieName = (String) tmpObj[1];
Double tmpThickness = Double.parseDouble(getSpecie().getThicknessFromSpecie(tmpSpecieName));
String tmpLength = getSpecie().getLengthFromSpecie(tmpSpecieName);
String tmpGradeName = (String) tmpObj[2];
String tmpShift = (String) tmpObj[3];
tmpSpecieName = getSpecie().getSpecieFromSpecie(tmpSpecieName);
//// END OF ALL ITEMS PULLED FROM DATABASE
if (grade != pullGradeName(tmpGradeName) && grade != null) {
System.out.println("Count:" + standingCount + "Grade:" + tmpGradeName + "--" + "Specie" + tmpSpecieName + "Shift:" + tmpShift + "Thickness:" + tmpThickness + "Length:" + tmpLength + "SpecieNAme:" + tmpSpecieName);
// do previous insert
grade = pullGradeName(tmpGradeName);
} else if (grade != pullGradeName(tmpGradeName) && grade == null) {
grade = pullGradeName(tmpGradeName);
} else if (grade == pullGradeName(tmpGradeName)) {
standingCount = standingCount + tmpCount;
}
System.out.println("Count:" + tmpCount + "Grade:" + tmpGradeName + "--" + "Specie" + tmpSpecieName + "Shift:" + tmpShift + "Thickness:" + tmpThickness + "Length:" + tmpLength + "SpecieNAme:" + tmpSpecieName);
}
}
Check the SQL that is generated, and the tables you are querying over. As the query requires inner joins, if one of the tables was cleared, it would return no results. If you want to get a 0 count, you need to use an outer join syntax which isn't possible in JPA unless you use object level mappings:
"SELECT COUNT(s) pCount,"
+ "p.nameText,"
+ "g.nameText,"
+ "t.shiftID"
+ " FROM Sheets s outer join s.specialNameIndex p,"
+ " outer join s.gradeNameIndex g, outer join s.shiftIndex t"
+ " WHERE s.createdLocal > :start and s.createdLocal < :end"
+ " GROUP BY p.nameText , g.nameText , t.shiftID";
I had problems with the slowness of this code.
I tried to optimize it for make it faster but I could not.
Can you give me some suggestions?
This is the part of the java code:
public ArrayList<String> kcal_spu(String tipo_pasto, String data) {
kcal_spuntino = new ArrayList<String>();
id_cibi_colazione = DiarioDataBaseAdapter.getId(tipo_pasto, data);
porzione_n2_colazione = DiarioDataBaseAdapter.getPorzione(tipo_pasto, data);
ArrayList<?> kcal2 = DiarioDataBaseAdapter.getKcal2(tipo_pasto, data);
int size = id_cibi_colazione.size();
int j = 0;
for (int i = 0; i < size; i++) {
double num_porz = Double.parseDouble(porzione_n2_colazione.get(j));
String getkcal = FoodDataBaseAdapter.getKcal(id_cibi_colazione.get(j));
String id_check = id_cibi_colazione.get(j);
if (id_check.equals("null")) {
double kcal_porz = Double.parseDouble(kcal2.get(j).toString());
double result = num_porz * kcal_porz;
kcal_spuntino.add("" + (int) result);
j = j + 1;
} else {
double kcal_porz = Double.parseDouble(getkcal);
double result = num_porz * kcal_porz;
kcal_spuntino.add("" + (int) result);
j = j + 1;
}
}
return kcal_spuntino;
}
This is the function DiarioDataBaseAdapter.getId:
public ArrayList getId(String tipo_pasto, String data) {
ArrayList array_list = new ArrayList();
Cursor res = db.rawQuery("SELECT ID_CIBO from Diario_Cibo where TIPO_PASTO = '" + tipo_pasto + "' AND DATA = '" + data + "'", null);
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex("ID_CIBO")));
res.moveToNext();
}
if (res != null) {
res.close();
}
return array_list;
}
This is the function DiarioDataBaseAdapter.getPorzione:
public ArrayList getPorzione(String tipo_pasto, String data) {
ArrayList array_list = new ArrayList();
Cursor res = db.rawQuery("SELECT PORZIONE from Diario_Cibo where TIPO_PASTO = '" + tipo_pasto + "' AND DATA = '" + data + "'", null);
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex("PORZIONE")));
res.moveToNext();
}
if (res != null) {
res.close();
}
return array_list;
}
This is the function DiarioDataBaseAdapter.getKcal2:
public ArrayList getKcal2(String tipo_pasto, String data) {
ArrayList array_list = new ArrayList();
Cursor res = db.rawQuery("SELECT KCAL from Diario_Cibo where TIPO_PASTO = '" + tipo_pasto + "' AND DATA = '" + data + "'", null);
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex("KCAL")));
res.moveToNext();
}
if (res != null) {
res.close();
}
return array_list;
}
Thanks in advance.
Your database queries will be the slowest part and there are three of them. Without profiling the code I would start with looking at the database selects and the tables to make sure they have the correct indexes on them for efficient searching.
Otherwise even putting a simple nanosecond timer in the code will give you an idea of what is taking the most time.
eg.
System.nanoTime():
long start = System.nanoTime();
// do stuff
long end = System.nanoTime();
System.out.println("time " + (end -start));
I want to check if id is exists in SQLIte DB then update that id , if id is not present in SQLite then insert that id. How can i do this. Can someone help how to work with.Thanks to Appreciate.
Here is my Activity code
public void saveResult()
{
AnswerOptions = (RadioButton) findViewById(Options_RadioGroup.getCheckedRadioButtonId());
String str_AnswerOptions = AnswerOptions.getText().toString().trim();
System.out.println("rbVal = " + str_AnswerOptions);
if (str_AnswerOptions.equals(((Datastructure) Vectore_mquestionDatabaseStructure .get(StaticClass.QuestionNumber)).Answer))
{
if (!StaticClass.isTest)
{
try
{
DatabaseHelper databaseHelper = new DatabaseHelper(this.getApplicationContext());
sqdb = databaseHelper.getWritableDatabase();
String strCountQuery = "SELECT question_id, question_type , question_no FROM question_answers where question_id = " + convertVector ;
Cursor cur = sqdb.rawQuery(strCountQuery , null);
if (cur.moveToFirst())
{
int iCount = cur.getCount();
System.out.println("iCount = " + iCount);
if(cur.getCount() <= 0 )
{
String strstrqueType = txtViewQuestiontype.getText().toString().trim();
String str_que = txtViewQuestion.getText().toString().trim();
String str_marks = "1";
databaseHelper.insertQueDetails(strQueLimit, strstrqueType, str_que, str_AnswerOptions, str_marks , strOption_Id);
System.out.println("strQueLimit = " + strQueLimit + ", strstrqueType = " + strstrqueType +" , str_que = " + str_que + " , str_AnswerOptions = " + str_AnswerOptions + ", str_marks = " + str_marks + ", strOption_Id = " + strOption_Id);
Toast.makeText(this, " Right Answer ", Toast.LENGTH_LONG).show();
}
else
{
cur.moveToFirst();
qid = cur.getInt(cur.getColumnIndex("question_id"));
String strQue_Type = cur.getString(cur.getColumnIndex("question_type"));
String strQue_Nomber = cur.getString(cur.getColumnIndex("question_no"));
System.out.println("qid in checkUpdateTable() = " + qid);
System.out.println("strQue_Type in checkUpdateTable() = " + strQue_Type);
System.out.println("strQue_Nomber in checkUpdateTable()= " + strQue_Nomber);
QueId = qid;
databaseHelper.updateAnswerRow(qid, str_AnswerOptions);
}
}
}
catch (SQLiteException se )
{
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
finally
{ if (sqdb != null) { sqdb.close();} }
}
}
}
You need to set a constraint on the table to trigger a "conflict" which you then resolve by doing a replace:
CREATE TABLE data (id INTEGER PRIMARY KEY, question_id INTEGER, question_type TEXT, question_no TEXT);
CREATE UNIQUE INDEX data_idx ON data(question_id);
Then you can issue:
INSERT OR REPLACE INTO data VALUES (NULL, 1, 2, 3);
INSERT OR REPLACE INTO data VALUES (NULL, 2, 2, 3);
INSERT OR REPLACE INTO data VALUES (NULL, 1, 2, 5);
I got another weird problem which i just don't understand.
I am saving doubles into my database with max. 3 digits after the decimal point.
The doubles are stored in the database correctly but when i am trying to fetch the data and add/multiply the stored doubles i get a weird problem
If i am storing data with only one digit after the decimal point, everything works fine and i get the right results.
But if i am adding 2 or more digits after the decimal point my whole calculations won't work. I get no results into my TextViews.
I'm trying to understand whats happening for hours now but i just don't see why this happens.
Here is the code:
My MainActivity, where the TextViews get the Text:
public void calc() {
// Get text for the statistics
amount_widget.setText(db.getSearchResult("amount", 0));
lpayment_widget.setText(db.getSearchResult("price", 0));
tpayment_widget.setText(db.getSearchResult("total_price", 1));
mileage_widget.setText(db.getSearchResult("mileage", 2));
trefueled_widget.setText(db.getSearchResult("amount", 1));
// Calculate text for the consumption
double consumption = (Double.parseDouble(db
.getSearchResult("amount", 1)) / Double.parseDouble(db
.getSearchResult("mileage", 2))) * 100;
consumption = Math.round(consumption * 100) / 100.0;
consumption_widget.setText(consumption + "L /100km");
}
My Database Activity where the code is being fetched and calculated:
public String getSearchResult(String sql, int cmd) {
if (cmd == 0) {
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
String tmp = cursor.moveToFirst() ? cursor.getString(0) : "0";
cursor.close();
return tmp;
} else if (cmd == 1) {
double sum = 0;
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
String idQuery = "SELECT _id FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
Cursor id = db.rawQuery(idQuery, null);
// berechnung
cursor.moveToFirst();
id.moveToFirst();
int maxId = Integer.parseInt(id.getString(0));
for (int i = 0; i < maxId; i++) {
double tmp = Integer.parseInt(cursor.getString(0));
sum = sum + tmp;
cursor.moveToNext();
}
cursor.close();
id.close();
return String.valueOf(sum);
} else if (cmd == 2 && sql == "mileage") {
double sum = 0;
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
String idQuery = "SELECT _id FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
Cursor id = db.rawQuery(idQuery, null);
// berechnung
cursor.moveToFirst();
id.moveToFirst();
int maxId = Integer.parseInt(id.getString(0));
if (maxId > 1) {
int array[] = new int[maxId];
// Array füllen
for (int i = 0; i < maxId; i++) {
array[i] = Integer.parseInt(cursor.getString(0));
// sum = sum + tmp;
cursor.moveToNext();
}
for (int k = 1; k < maxId; k++) {
double tmp;
tmp = array[k] - array[k - 1];
sum = sum + tmp;
}
cursor.close();
id.close();
return String.valueOf(sum);
} else {
return "--";
}
}
return "Wrong CMD";
}
Maybe you're making a simple mistake. Instead of:
consumption_widget.setText(consumption + "L /100km");
try this:
consumption_widget.setText(String.valueOf(consumption) + "L /100km");
I found my mistake.
In my DbAdapter class within the getSearchResult() method i have the following statement:
for (int i = 0; i < maxId; i++) {
double tmp = Integer.parseInt(cursor.getString(0));
sum = sum + tmp;
cursor.moveToNext();
}
Here I am trying to parse an Integer but write it into a double. This is why the calculations couldn't be executed. I need to parse a double like this:
for (int i = 0; i < maxId; i++) {
double tmp = Double.parseDouble(cursor.getString(0));
sum = sum + tmp;
cursor.moveToNext();
}
this is the solution to my problem