protected void cal() {
// TODO Auto-generated method stub
Company obj1 = new Company();
String val1 = obj1.getCompanyID();
edite.getText().toString();
Cursor sumw = dbObject.rawQuery(
"SELECT sum(Volume)FROM SHARE WHERE _id2 LIKE ?",
new String[] { "%" + val1 + "%" });
sumw.moveToFirst();
editT.setText(sumw.getString(0));
}
i want to get the value of the EditText value, to multiply with the selected sum value of the query
Try this,
protected void cal() {
// TODO Auto-generated method stub
Company obj1 = new Company();
String val1 = obj1.getCompanyID();
int n = Integer.parseInt(edite.getText().toString());
Cursor sumw = dbObject.rawQuery(
"SELECT sum(Volume)FROM SHARE WHERE _id2 LIKE ?",
new String[] { "%" + val1 + "%" });
sumw.moveToFirst();
editT.setText(sumw.getString(0)*n);
}
If they're integer values, try this:
int multiplierOne = Integer.parseInt(edite.getText().toString());
....
int multiplierTwo = sumw.getInt(0);
int product = multiplierOne * multiplierTwo;
editT.setText("" + product);
Related
I have a sqlite database which I want to query and retrieve some values if they exist, in case they don't exist make a toast.
For this porpoise I have the next code which will call a function to create the cursor and in case this is not empty it will retrieve the information in the edittext otherwise it will make a toast:
private String lookforelementID() {
// TODO Auto-generated method stub
int ElementRequest=0;
ElementRequest = Integer.parseInt(eTElementNumb.getText().toString());
try{
database db = new database(this);
db.open();
String passedReg = getIntent().getStringExtra(MainActivity.ID_STUDY);
String returnedInfo1 = db.elementID(ElementRequest, passedReg, 0);
if (returnedInfo1.matches("")){
Toast.makeText(getApplicationContext(), "No Exist this Element", Toast.LENGTH_SHORT).show();
}else{
eTIDElement.setText(returnedInfo1);
String returnedInfo2 = db.elementID(ElementRequest, passedReg, 2);
eTElementCode.setText(returnedInfo2);
String returnedInfo3 = db.elementID(ElementRequest, passedReg, 3);
eTElementDecription.setText(returnedInfo3);
}
db.close();
}catch(ClassCastException e){
e.printStackTrace();
}
eTElementNumb.setText("");
return null;
}
The function with the cursor is as follows:
public String elementID(int elementRequest, String idStudy, int i) {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWELEMENTID, KEY_STUDYID, KEY_ELEMENTCODE, KEY_ELEMENTNAME};
Cursor c = ourDatabase.query(DATABASE_TABLEELEMENTS, columns, KEY_ELEMENTCODE + "=" + elementRequest + " AND " + KEY_STUDYID + "=" + idStudy, null, null, null, null);
String ElementInformation = null;
if(c != null){
c.moveToFirst();
ElementInformation = c.getString(i);
}
return ElementInformation;
}
I have a code that would give me the sum of a column in a database, i have done the crud, but now i would like to do a search by a the name of a column and show the sum of all the records(that have the same name) and show in a textfield.
the following is my DatabasdeHandler:
public Cursor getSingleDespesaSum(String date) {
SQLiteDatabase db = this.getReadableDatabase();
int sum = 0;
Cursor cursor = db.rawQuery(
"select sum(valor) from despesas WHERE data = ?", null);
if (cursor.moveToFirst()) {
do {
sum = cursor.getInt(0);
} while (cursor.moveToNext());
}
return cursor;
}
And this is my activity;
btGetSum.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
try {
dia = (String) spinDia.getSelectedItem();
mes = (String) spinMes.getSelectedItem();
ano = (String) spinAno.getSelectedItem();
String dataSendTo = dia + "/" + mes + "/" + ano;
dbhelper.getSingleDespesaSum(dataSendTo); //missing code
} catch (Exception erro) {
mensagemExibir("Erro Ao Buscar", "" + erro.getMessage());
}
}
});
}
Content Providers are the way to go, don't access your database directly like this.
But... to answer your current question:
SQLiteDatabase db = this.getReadableDatabase();
int sum = 0;
Cursor cursor = db.rawQuery(
"select value from table WHERE date= ?", null);
while (cursor.moveToNext()) {
//Increment your counter
sum += cursor.getInt(cursor.getColumnIndex("value");
};
here is my code for using that string
String item = item1.getText().toString();
item = item.toLowerCase();
String date = getDate();
Datahelper edited = new Datahelper(this);
edited.open();
String returnedprice = edited.getprice(item,date);
String returneddetail = edited.getdetail(item,date);
edited.close();
price.setText(returnedprice);
details.setText(returneddetail);
and this is my method in sqlite
public String getprice(String item ,String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item,date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
String price = c.getString( c.getColumnIndex(KEY_PRICE));
return price;
}
return null;
}
public String getdetail(String item, String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item, date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
return detail;
}
return null;
}
my app gets crash when using this code,, i dont know waht's wrong in the code if anyone need i can post the whole code pls help me
the column index has nothing in common with the returned set:
String price = c.getString( c.getColumnIndex(KEY_PRICE));
in c you have only 5 columns (0 to 4) ( String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS}; ) and c.getColumnIndex(KEY_PRICE) return the actual index in the table which is probably higher than 4.
instead of
String price = c.getString( c.getColumnIndex(KEY_PRICE));
try
String price = c.getString(3);
the corect code is -
public String getprice(String item ,String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item,date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
c.moveToFirst();
String price = c.getString( c.getColumnIndex(KEY_PRICE));
return price;
}
return null;
}
public String getdetail(String item, String date) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};
String whereClause = KEY_CATEGORY + " = ? and " + KEY_DATE + " = ?";
String[] whereArgs = {item, date};
Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
if(c!=null){
c.moveToFirst();
String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
return detail;
}
return null;
}
I am trying, unsucessfully, to query my database to find the maximum 'area number', in my areas table for a certain inspection, so that I can set the text in a form to the next area number.
The database table consists of four columns; _id, inpsection_link, area_number, area-reference.
I have created the following in my database helper class (using this post as a guide: SQLiteDatabase.query method):
public int selectMaxAreaNumber (long inspectionId) {
String inspectionIdString = String.valueOf(inspectionId);
String[] tableColumns = new String[] {
AREA_NUMBER,
"(SELECT max(" + AREA_NUMBER + ") FROM " + AREAS_TABLE + ") AS max"
};
String whereClause = INSPECTION_LINK + " = ?";
String[] whereArgs = new String[] {
inspectionIdString
};
Cursor c = rmDb.query(AREAS_TABLE, tableColumns, whereClause, whereArgs,
null, null, null);
int maxAreaNumber = c.getColumnIndex("max");
return maxAreaNumber;
}
Which I then call in the areaEdit class as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
areaId = i.getLongExtra("Intent_AreaID", -1);
if (areaId == -1) {
nextAreaNumber = rmDbHelper.selectMaxAreaNumber(inspectionId) + 1;
Toast.makeText(getApplicationContext(), String.valueOf(nextAreaNumber),
Toast.LENGTH_LONG).show();
}
setContentView(R.layout.edit_area);
setUpViews();
populateFields();
setTextChangedListeners();
}
However, it just returns 1 everytime (even if there are numbers higher than that stored in the database).
Confused.com!! Any help much appreciated.
Your issue is here :
int maxAreaNumber = c.getColumnIndex("max");
You're getting the column index of max, which is 1 because you only have one column in your query.
Instead, do something like this :
int maxAreaNumber = 0;
if(c.moveToFirst())
{
maxAreaNumber = c.getInt(1);
// or cleaner
maxAreaNumber = c.getInt(c.getColumnIndex("max"));
}
else
// no data in cursor
public String val2;
public String val3;
private void sumsp() {
// TODO Auto-generated method stub
Company obj1 = new Company();
String val1 = obj1.getCompanyID();
Cursor sumw = dbObject.rawQuery(
"SELECT avg(Price) FROM SHARE WHERE _id2 LIKE ?",
new String[] { "%" + val1 + "%" });
sumw.moveToFirst();
val2 = sumw.getString(0);
String result = val2+val3;
text2.setText(result);
}
private void sum() {
// TODO Auto-generated method stub
Company obj1 = new Company();
String val1 = obj1.getCompanyID();
Cursor sum = dbObject.rawQuery(
"SELECT Sum(Volume) FROM SHARE WHERE _id2 LIKE ?",
new String[] { "%" + val1 + "%" });
sum.moveToFirst();
val3 = sum.getString(0);
text.setText(sum.getString(0));
}
what i want to do is... get the val3 value and multiply it in the sumsp() val2.
result = (val2*val3*1.02252544)/val2;
and display it on a textview.
You can compute the whole thing with one query:
Cursor sumw = dbObject.rawQuery(
"SELECT avg(Price) * sum(Volume) FROM SHARE WHERE _id2 LIKE ?",
new String[] { "%" + val1 + "%" });
So you probably don't even need val1, val2, sumsp(), or sum() at all.