Wrong select query SQLite?
-
First, I have a button that calls 2 functions. First function (calculateMedieSim()) makes an operation with two number and the second one (selectData()) selects data from tabel. `totalsim` = 15 and `totalsimyes` = 8. The result should be 53,3 but it shows 0 Why?
private void button1_Click(object sender, EventArgs e)
{
selectData();
calculateMedieSim();
}
private void selectData()
{
string select = "SELECT * FROM accounts WHERE id=1";
using (SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection()))
{
using (SQLiteDataReader read = cmd.ExecuteReader())
{
if (read.Read())
{
totalsim = (int)read["totalsim"];
totalsimyes = (int)read["totalsimyes"];
}
}
}
}
private void calculateMedieSim()
{
medie_sim = (totalsimyes / totalsim) * 100;
sansa_examen = Math.Round(medie_sim, 2);
Console.WriteLine(sansa_examen);
} -
First, I have a button that calls 2 functions. First function (calculateMedieSim()) makes an operation with two number and the second one (selectData()) selects data from tabel. `totalsim` = 15 and `totalsimyes` = 8. The result should be 53,3 but it shows 0 Why?
private void button1_Click(object sender, EventArgs e)
{
selectData();
calculateMedieSim();
}
private void selectData()
{
string select = "SELECT * FROM accounts WHERE id=1";
using (SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection()))
{
using (SQLiteDataReader read = cmd.ExecuteReader())
{
if (read.Read())
{
totalsim = (int)read["totalsim"];
totalsimyes = (int)read["totalsimyes"];
}
}
}
}
private void calculateMedieSim()
{
medie_sim = (totalsimyes / totalsim) * 100;
sansa_examen = Math.Round(medie_sim, 2);
Console.WriteLine(sansa_examen);
}the only thing I see wrong is (and I presume you've defined the variables this way as well) ...
totalsim = (int)read...
totalsimyes = (int)read...so, using integer math, whats going to happen when you divide 8 by 15, hmmmm ?
-
the only thing I see wrong is (and I presume you've defined the variables this way as well) ...
totalsim = (int)read...
totalsimyes = (int)read...so, using integer math, whats going to happen when you divide 8 by 15, hmmmm ?
-
the only thing I see wrong is (and I presume you've defined the variables this way as well) ...
totalsim = (int)read...
totalsimyes = (int)read...so, using integer math, whats going to happen when you divide 8 by 15, hmmmm ?
-
defined by int type ? yes, because you've defined them as such - define them as double for example
-
If I change code to:
medie_sim = (totalsim / totalsimyes) * 100;
medie_sim = Math.Round(medie_sim, 2);
Console.WriteLine(medie_sim);I get 100
how is medie_sim defined ? let me guess - also an Int ?
-
how is medie_sim defined ? let me guess - also an Int ?
-
defined by int type ? yes, because you've defined them as such - define them as double for example
-
It's the same problem as your other code - integer division.
15 / 8 = 1.875
, but since the variables are integers, only the integer part is used:15 / 8 = 1
- You then multiply that result by
100
, which (unsurprisingly) gives a final result of100
. - You then store that final result in a
double
, but it's too late - you've already lost the fractional part at step 1.
You need to convert one of the values to a floating-point type before the division:
medie_sim = ((double)totalsim / totalsimyes) * 100;
// or:
// medie_sim = (totalsim / (double)totalsimyes) * 100;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer