Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Wrong select query SQLite?

Wrong select query SQLite?

Scheduled Pinned Locked Moved C#
databasesqlitequestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DPaul1994
    wrote on last edited by
    #1

    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);
    }

    G 1 Reply Last reply
    0
    • D DPaul1994

      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);
      }

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      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 ?

      D 2 Replies Last reply
      0
      • G Garth J Lancaster

        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 ?

        D Offline
        D Offline
        DPaul1994
        wrote on last edited by
        #3

        those 2 variables are defined by int type

        G 1 Reply Last reply
        0
        • G Garth J Lancaster

          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 ?

          D Offline
          D Offline
          DPaul1994
          wrote on last edited by
          #4

          If I change code to:

          medie_sim = (totalsim / totalsimyes) * 100;
          medie_sim = Math.Round(medie_sim, 2);
          Console.WriteLine(medie_sim);

          I get 100

          G 1 Reply Last reply
          0
          • D DPaul1994

            those 2 variables are defined by int type

            G Offline
            G Offline
            Garth J Lancaster
            wrote on last edited by
            #5

            defined by int type ? yes, because you've defined them as such - define them as double for example

            D 1 Reply Last reply
            0
            • D DPaul1994

              If I change code to:

              medie_sim = (totalsim / totalsimyes) * 100;
              medie_sim = Math.Round(medie_sim, 2);
              Console.WriteLine(medie_sim);

              I get 100

              G Offline
              G Offline
              Garth J Lancaster
              wrote on last edited by
              #6

              how is medie_sim defined ? let me guess - also an Int ?

              D 1 Reply Last reply
              0
              • G Garth J Lancaster

                how is medie_sim defined ? let me guess - also an Int ?

                D Offline
                D Offline
                DPaul1994
                wrote on last edited by
                #7

                no, medie_sim is double

                Richard DeemingR 1 Reply Last reply
                0
                • G Garth J Lancaster

                  defined by int type ? yes, because you've defined them as such - define them as double for example

                  D Offline
                  D Offline
                  DPaul1994
                  wrote on last edited by
                  #8

                  Aaaa yes, there was the problem. Thanks :)

                  1 Reply Last reply
                  0
                  • D DPaul1994

                    no, medie_sim is double

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    It's the same problem as your other code - integer division.

                    1. 15 / 8 = 1.875, but since the variables are integers, only the integer part is used: 15 / 8 = 1
                    2. You then multiply that result by 100, which (unsurprisingly) gives a final result of 100.
                    3. 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

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups