Concatenation overwrites in for loop - android

I partially want to concat a string inside a for loop (inside a while loop). However, when it's concatenating inside the for loop, it's overwriting the past concatenating.
I am quite new to programming, so I don't get why it's working fine outside a for loop and overwriting itself inside of it.
String uitslagtxt;
uitslagtxt = "Datum" + "|" + "Teamnamen" + "|" + "Uitslag" + "|" + "(Ruststand)"+"\n";
for (int i=0; i<1; i++){
uitslagtxt = uitslagtxt.concat(row[1]+"|" +row[2]+"|"+row[3]+"|"+row[4].replace("\"","")+"\n");
out.println("uitslagtxt in for loop: " + uitslagtxt);
}
So instead of giving 4 lines for example (which are all visible in the out.println check, see below),
Datum | Teamnamen | Uitslag | (Ruststand)
1 jun. 2017 | Thuis - Uit | 2 - 2 | (2 - 0)
uitslagtxt in for loop: Datum | Teamnamen | Uitslag | (Ruststand)
1 jun. 2017 | Thuis - Uit | 1 - 1 | (1 - 0)
uitslagtxt in for loop: Datum | Teamnamen | Uitslag | (Ruststand)
1 jun. 2017 | Thuis - Uit | 3 - 3 | (3 - 0)
it's only writing 2. How come?
Datum | Teamnamen | Uitslag | (Ruststand)
1 jun. 2017 | Thuis - Uit | 3 - 3 | (3 - 0)

It's overwriting the string because it is inside a while loop.
You declare the string inside while block, so every time it is repeated the variable is declared again.
Move the string declaration and initialization before starting the while loop.
String uitslagtxt;
uitslagtxt = "Datum" + "|" + "Teamnamen" + "|" + "Uitslag" + "|" + "(Ruststand)"+"\n";
while (condition) {
// your code
}

Related

SQLite SUM with hierarchy values

I have payment analysis app with the following simplified structure:
Payment
CREATE TABLE payments (
payment_Id TEXT NOT NULL,
payment_type INTEGER NOT NULL,
payment_name TEXT NOT NULL,
payment_category INTEGER NOT NULL,
payment_date TEXT NOT NULL,
payment_cost TEXT NOT NULL, PRIMARY KEY(payment_Id))
Category
CREATE TABLE categories (
category_Id INTEGER NOT NULL,
idRoot TEXT NOT NULL,
category_Name TEXT NOT NULL,
category_Type INTEGER NOT NULL, PRIMARY KEY(category_Id))
Foreign key: categories.category_Id = payment_category
Categories are structured in a tree hierarchy:
IdRoot
category_Id
Name
-
0
Unknown
0.
1
Expenses
0.1.
2
House
0.1.
3
Transport
0.1.
4
Travel
0.1.2.
5
Internet
0.1.2.
6
Groceries
0.1.2.
7
Water
0.1.3.
8
Car
0.1.3.
9
Public Transport
0.1.3.8.
10
Gasoline
0.1.3.8.
11
Tolls
...
...
....
Payments can be assigned to any category (except category_Id = 0).
I want to know if it is possible using just SQLite (compatible with Android is appreciated) to get all the children categories of a parent category and the sum of the payment_cost assign to every category in that level and its children.
Example: given category Transport(category_Id: 3), sqlite will return Car and Public Transport and their sums but the sum of Car will also include the payment_cost assign to Gasoline and Tolls something like this
Input: category_Id: 3
Data:
| payment_name | payment_cost| payment_category |
| ------------ | ---------- | ----------------- |
| Gas Station | 50,3 | 10 |
| Metro | 2,4 | 9 |
| Tool NY - Phil | 21 | 11 |
| Car cleaning | 11 | 8 |
Result
category_name
Total
Car
82,3 (50,3 + 21 + 11)
Public Transport
2,4
Thank you in advance for your time and knowledge
You can use like to get the matching roots:
select sum(payment_cost)
from payments p join
categories c
on p.payment_category = c.category_id cross join
categories c3
on c3.category_id = 3
where p.idroot like c3.idroot || '%'

Android Database Query Cross-row Check

I am working on a project which requires me to do some query in android database. However, I am completely new to database and I am a little confused at how to deal with such scenarios. Here's a rough example to show my question.
For a table like this:
TableA:
--------------------------------------------------------
| _id | name | mimetype | data1 | data2 | data3 |
--------------------------------------------------------
| 1 | user1 | mime1 | val1 | val2 | x |
--------------------------------------------------------
| 2 | user1 | mime2 | val3 | val4 | y |
--------------------------------------------------------
| 3 | user1 | mime3 | val8 | val5 | a |
--------------------------------------------------------
| 4 | user2 | mime2 | val6 | val7 | q |
--------------------------------------------------------
| 5 | user2 | mime3 | val9 | val10 | a |
--------------------------------------------------------
| 6 | user3 | mime1 | val11 | val12 | b |
--------------------------------------------------------
Basically, I want to do a query to return data from columns of name, data1, data2 from TABLE A when it fits the following criteria:
mimetype = mime2
there's another row in the table that has the same name and its mimetype = mime3 and data3 = a
I am wondering how to do this in one query.
If anyone can give some direction on how to figure this out, it will be great! Thanks!
This can be done with the EXISTS operator and a correlated subquery:
SELECT ...
FROM TableA
WHERE mimetype = 'mime2'
AND EXISTS (SELECT 1
FROM TableA AS T2
WHERE T2.name = TableA.name
AND T2.mimetype = 'mime3'
AND T2.data3 = 'a');

How to add values from different table that has the same id number?

I have 7 tables, each table has a column for total I need to get the total of each table which has the same id number, these are 3 sample tables:
clean table
clean_id | labor_expense | machine_expense | diesel_expense | clean_total
1 | 1000 | 2000 | 500 | 3500 2 | 2000 | 1000 | 1000 | 4500
plant_table
plant_id | labor_expense | machine_expense | plant_expense | plant_total
1 |
1000 |
2000 |
500 | 3500 2 | 2000 | 1000 | 1000 | 4500
fertilize_table
fertilize_id | labor_expense | machine_expense | fertilizer_expense | fertilize_total
1 |
1000 |
2000 |
500 | 3500 2 | 2000 | 1000 | 1000 | 4500
How can I get the total of clean_total, plant_total and fertilize_total that have the same id number?
I was thinking to save it to another table like total_expense but I only get the total of the first id number
total_expenses
total_id | clean_total | plant_total | fertilize_total | total_expenses
1 |
3500 |
3500 |
3500 | 10500 2 | 4500 | 4500 | 4500 | 13500
If the rows for each ID are known to exist in all tables, you can simply join them:
SELECT clean_id AS total_id,
clean_total + plant_total + fertilize_total AS total_expenses
FROM clean_table
JOIN plant_table ON clean_id = plant_id
JOIN fertilize_table ON clean_id = fertilize_id

Use second cursor in cursorAdapter

I have a database like
--------------------------------------------------
| _id| poi_id | poi_name |poi_address |
--------------------------------------------------
| 1 | 101 | sight_1 |sight_1_adr |
--------------------------------------------------
| 2 | 101 | sight_2 |sight_2_adr |
--------------------------------------------------
| 3 | 100 | sight_3 |sight_3_adr |
--------------------------------------------------
| 4 | 101 | sight_4 |sight_4_adr |
--------------------------------------------------
| 5 | 100 | sight_5 |sight_5_adr |
--------------------------------------------------
First of all , i want to create a listview and group my data by "poi_id".
I use the following query:
"SELECT* FROM poi_table GROUP BY poi_id"
I use a cursor with that query and i create the list view as:
entry.open();
Cursor cursor = entry.getData();
adapter = new CustomListAdapter(this.getActivity(), cursor);
setListAdapter(adapter);
My problem is that, in every list row, i would like to print also the poi_name of every poi_id.So iwould like to have something like:
-------------------------------
-101-
sight 1
sight 2
sight 4
-------------------------------
-100-
sight 3
sight 5
-------------------------------
etc
This is where i stack!
In my CustomCursorAdapter, i get the poi_id as:
String ID = cursor.getString(cursor
.getColumnIndex(db.POI_ID));
and then i use a second cursor to get the data like:
Cursor c2 = entry.getDatabyID(ID);
for (int i = 0; i < entry.fetchPlacesCountByID(ID); i++) {
c2.moveToPosition(i);
String name= c2
.getString(c2
.getColumnIndex(db.POINT_NAME));}
and i create dynamically a new textView and i set the string name there as text.
How can i fix it?That way doesnt crash but i get as many poi_names as i scroll down..

SQLite3 joining rows to form a single row

I am building an android application which queries a database for a bus number, and bus departure time.
The tables are thus -
Table 1: Route - _id, routenumber
Table 2: Routetimedetails - _id, routetime, routeid
My current query is this
select routenumber, route._id, routetime from route, routetimedata where route._id = 2 and routeid = route._id
and the result shows up like this
>
routenumber | route._id | routetime
-------------------------------------
BIAS10 | 2 | 0945
BIAS10 | 2 | 1810
BIAS10 | 2 | 1945
BIAS10 | 2 | 0710
is there a way in sqlite3 to show the result in a single column like this -
BIAS10 | 2 | 0710, 0945, 1810, 1945
Thanks
Mukul
I find that the Group_Concat() function does this easily
select routenumber, route._id, Group_concat(routetime) from route, routetimedata where route._id = 2 and routeid = route._id

Categories

Resources