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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Extract data from database

Extract data from database

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structureshelp
21 Posts 4 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.
  • K Kevin McFarlane

    Using a CArray (or STL vector) is far easier than trying to get the data from a database. There will be examples aplenty on codeproject However, if you really want to use a database, what database do you have - MS Access? You don't say whether you're using MFC or just doing straight C++. If MFC then probably the easiest method is to use the DAO classes. Though this might be an old-fashioned way of going about it these days. You could use ADO but, if I recall, MFC does not have native ADO classes - though it's still possible to use it. the point is that these methods are all somewhat more elaborate than using a database. Kevin

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

    i am using MFC but don't have to show the values out, just need to store into array and calculate an end result we i want to acheive. so using CArray is the only solution and easiest?

    D 1 Reply Last reply
    0
    • D dreamerzz

      I have read up on this and tried but dun seems to get what i want to achieve.. there are some errors.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #9

      dreamerzz wrote: I have read up on this... So this article was of no help? dreamerzz wrote: there are some errors. And those are?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      D 1 Reply Last reply
      0
      • D dreamerzz

        i am using MFC but don't have to show the values out, just need to store into array and calculate an end result we i want to acheive. so using CArray is the only solution and easiest?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #10

        dreamerzz wrote: so using CArray is the only solution... No, of course not. It's just one of many implementations. From what information you've provided, it sounds like you simply need to store the numbers in a text file. Then you could process them with something like:

        CStdioFile file("", CFile::modeRead);
        CString strLine;
        int nSum = 0,
        nLineCount = 0;

        while (file.ReadString(strLine) != FALSE)
        {
        nSum += atoi(strLine);
        nLineCount++;
        }

        TRACE("The sum is %d\nThe average is %f\n", nSum, (double) nSum / nLineCount);


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        D 1 Reply Last reply
        0
        • D David Crow

          dreamerzz wrote: I have read up on this... So this article was of no help? dreamerzz wrote: there are some errors. And those are?


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          D Offline
          D Offline
          dreamerzz
          wrote on last edited by
          #11

          DAMAGE: after Client block (#74) at 0x00432C50 i have tried a lot of other methods, when i compile it, there is no error but when i wan to build, it show me this dialog box

          D 1 Reply Last reply
          0
          • D David Crow

            dreamerzz wrote: so using CArray is the only solution... No, of course not. It's just one of many implementations. From what information you've provided, it sounds like you simply need to store the numbers in a text file. Then you could process them with something like:

            CStdioFile file("", CFile::modeRead);
            CString strLine;
            int nSum = 0,
            nLineCount = 0;

            while (file.ReadString(strLine) != FALSE)
            {
            nSum += atoi(strLine);
            nLineCount++;
            }

            TRACE("The sum is %d\nThe average is %f\n", nSum, (double) nSum / nLineCount);


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            D Offline
            D Offline
            dreamerzz
            wrote on last edited by
            #12

            i am using MFC window so is there a way to retrieve directly from the database itself?

            D 1 Reply Last reply
            0
            • D dreamerzz

              DAMAGE: after Client block (#74) at 0x00432C50 i have tried a lot of other methods, when i compile it, there is no error but when i wan to build, it show me this dialog box

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #13

              dreamerzz wrote: when i compile it, there is no error but when i wan to build, it show me this dialog box Are you differentiating between a compile and a build? If so, I can only assume that you mean build to be both a compile and a link. Yes?


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              D 1 Reply Last reply
              0
              • D David Crow

                dreamerzz wrote: when i compile it, there is no error but when i wan to build, it show me this dialog box Are you differentiating between a compile and a build? If so, I can only assume that you mean build to be both a compile and a link. Yes?


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                D Offline
                D Offline
                dreamerzz
                wrote on last edited by
                #14

                sorry for the confusion. There is a build and execute. When i build, there is no error and no warning. but when i execute, it show me the dialog box

                D 1 Reply Last reply
                0
                • D dreamerzz

                  i am using MFC window so is there a way to retrieve directly from the database itself?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #15

                  Yes, using the CDatabase and CRecordset classes.


                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                  D 1 Reply Last reply
                  0
                  • D dreamerzz

                    sorry for the confusion. There is a build and execute. When i build, there is no error and no warning. but when i execute, it show me the dialog box

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #16

                    Have you single-stepped through the code to see which is the offending statement? Up until you know the statement in question, we can't be of much help.


                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                    1 Reply Last reply
                    0
                    • D David Crow

                      Yes, using the CDatabase and CRecordset classes.


                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                      D Offline
                      D Offline
                      dreamerzz
                      wrote on last edited by
                      #17

                      ok. I think this is also the very last ans i would get. Although i don't really know how to go abt it. but still thanks for all the help that u have given.. thanks.

                      D 1 Reply Last reply
                      0
                      • G gUrM33T

                        Yes there is. Using the new operator. At run-time, find out the number of records, and then allocate an array long enough to hold values you want to retrieve. For example, assuming that you've 50 records, the code will look like this:

                        LONG lRecCount = 50; // you won't hardcode this, but get it at run-time from database
                        double *pdblValues = new double[lRecCount + 1];
                        for(LONG i = 0; i < lRecCount; i++)
                        pdblValues[i] = collect value from the field here;

                        One more thing, don't forget to delete[] pdblValue after you are finished with it. Hope that helps, Gurmeet


                        BTW, can Google help me search my lost pajamas?

                        My Articles: HTML Reader C++ Class Library, Numeric Edit Control

                        D Offline
                        D Offline
                        dreamerzz
                        wrote on last edited by
                        #18

                        Hi, I have tried out the coding that you have given me. but i dun seems to get the results also.. Anyway, thanks for the help given.

                        G 1 Reply Last reply
                        0
                        • G gUrM33T

                          Yes there is. Using the new operator. At run-time, find out the number of records, and then allocate an array long enough to hold values you want to retrieve. For example, assuming that you've 50 records, the code will look like this:

                          LONG lRecCount = 50; // you won't hardcode this, but get it at run-time from database
                          double *pdblValues = new double[lRecCount + 1];
                          for(LONG i = 0; i < lRecCount; i++)
                          pdblValues[i] = collect value from the field here;

                          One more thing, don't forget to delete[] pdblValue after you are finished with it. Hope that helps, Gurmeet


                          BTW, can Google help me search my lost pajamas?

                          My Articles: HTML Reader C++ Class Library, Numeric Edit Control

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #19

                          Gurmeet S. Kochar wrote: double *pdblValues = new double[lRecCount + 1]; Why the extra one?


                          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                          1 Reply Last reply
                          0
                          • D dreamerzz

                            ok. I think this is also the very last ans i would get. Although i don't really know how to go abt it. but still thanks for all the help that u have given.. thanks.

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #20

                            dreamerzz wrote: Although i don't really know how to go abt it In one of my earlier posts, I provided a link to a "how to" article. Once you go through those steps, not just glancing over them, you'll have a working example. Here are some others: http://www.codeproject.com/database/#ODBC http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vctutor98/html/\_gs\_creating\_a\_new\_database\_application.asp http://msdn.microsoft.com/library/en-us/vctutor98/html/\_gs\_about\_step\_1.asp?frame=true


                            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                            1 Reply Last reply
                            0
                            • D dreamerzz

                              Hi, I have tried out the coding that you have given me. but i dun seems to get the results also.. Anyway, thanks for the help given.

                              G Offline
                              G Offline
                              gUrM33T
                              wrote on last edited by
                              #21

                              Can you please provide us with some code of how you are doing this. It may be that you are doing something wrong in your code. Gurmeet


                              BTW, can Google help me search my lost pajamas?

                              My Articles: HTML Reader C++ Class Library, Numeric Edit Control

                              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