Dim strGuest As String = csr.GetString("Guest")
Dim strSQL As String = "SELECT SUM(CostPaid) FROM RentTable WHERE Guest = ?"
Dim TotalCostPaid As Double = SQL1.ExecQuerySingleResult2(strSQL, Array As String(strGuest))
Log(NumberFormat2(TotalCostPaid, 1, 2, 2, True))
String Literal
B4X:
Dim strGuest As String = csr.GetString("Guest")
Dim strSQL As String = $"SELECT SUM(CostPaid) FROM RentTable WHERE Guest = '${strGuest}'"$
Dim TotalCostPaid As Double = SQL1.ExecQuerySingleResult(strSQL)
Log(NumberFormat2(TotalCostPaid, 1, 2, 2, True))
Primary key is use to identify the record that is unique, meaning one guest has id = 1, another guest has id = 2.
If you want to develop database driven app, you must learn and have knowledge of SQL.
You need to understand the concept of Primary key, Foreign key, One-to-One relationship, One-to-Many, Many-to-Many relationship.
I can see why @Theera would get confused with the answer by @corwin42 , because the answer has nothing to do with PRIMARY KEY. If he wants to use the second solution given by corwin42, the code is still missing the WHERE clause to single out a given guest. Otherwise, it will return the total cost for each guest in the table grouped by guest. Your solution in post #4 is the correct one in this case.
The code in the first post starts a cursor over all guests and then in a second select it gets the sum(CostPaid) from the guest which is the current one in the cursor of the first select. I guess there is a loop in the code which walks over all guests. Otherwise it will not make much sense.
So I guess Theera wants to walk over all guests and get the sum() for all guests in the end. My select does this in only one statement, the other solution does this in 1+NumberOfGuests select statements. What do you think is more effective? I just want to make clear that the code in the first post can be optimized for speed and could be made simpler (you don't have to use parameters for the sql)