Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

c++ array loading from DB inheriting previous data when empty

so we have access to the database and we can read it and thats ok, we have the array working we can pass it from the main function to the get the data from the record set and that works
so we run it once and the data is  
"loftus , "electricity" ,1000" ,""
"loftus,"water","","899"
if you change the emptys to 0 it then behaves as it should but cannot always guarentee that a 0 will be stored in there
ect
but each rec after 0 rec count inherits the number from the previous if it is blank.
in the second rec case the 3rd space is blank so it will remain what it was prev untill it is over written by a new value. in this case 1000
this happens for all the records of the record set
so the 2nd record would end up being  "loftus,"water","1000","899" instead of "loftus,"water","","899"

this is the section of code doing most of the work
CRowset<CDynamicStringAccessor>* pRS = (CRowset<CDynamicStringAccessor>*)&cmd;
                                    // Loop through the rows in the result set.
                                    
                                    while (pRS->MoveNext() == S_OK)
                                    {

                                          for (int col=1; col <= (int)colCount; col++)
                                          {
                                                CHAR* szValue = "";
                                                szValue = cmd.GetString(col);
                                                cout<<" | "<<szValue;
                                                // PLACE IN HERE THE ARRAY TO COLLECT ALL THE DATA
                                    
                                                DataArray[rowCount][col] = "";
                                                DataArray[rowCount][col] = szValue;
                  
                                          }
                                          
                                          cout<<endl;
                                          rowCount++;
                                    }




#include "stdafx.h"
#include ".\accessmsdb.h"
#include  <atldbcli.h>
#include <iostream>
#include <string>
#include  <cstring>
#include <cstdio>
 #include      "StdAfx.h"


using std::string;
using namespace std;

accessMSdb::accessMSdb(void)
{
      
}

accessMSdb::~accessMSdb(void)
{
}


string accessMSdb::ExtractDataDB(string DataArray[][7])  //
{
      // ----------------------------------------------------------
      // build the array to hold the data
      const int rows = 50;
    const int cols = 8;
      string AccounFnd[rows][cols] ;
      

//        int height = 15;
//      int width = 10;
//        string** my2DArray =  accessMSdb::create2DArray(height, width);

      CoInitialize( NULL );
      // TODO: Please replace the sample code below with your own.
    HRESULT hr = 0;
    std::string str;
    int i=0, ret=0;
  //  std::string query;
      

      string DAM;
      //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
      //   Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;User Id=admin;Password=;
      LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;User Id=admin;;Password=;";

                  // To initialize the connection to a database using an OLE DB provider,
            // two ATL classes are needed: CDataSource and CSession.
            CDataSource dbDataSource;
            CSession dbSession;

            // Uses ATL's string conversion macros to convert between character encodings.
            USES_CONVERSION;

            // Open the connection and initialize the data source specified by the passed
            // initialization string.
            hr = dbDataSource.OpenFromInitializationString(lpcOleConnect);
              if (FAILED(hr))
            {
                  cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<endl;
            }
            else
            {
                  hr = dbSession.Open(dbDataSource);
                  if (FAILED(hr))
                  {
                        cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<endl;
                  }
                  else
                  {
                        CComVariant var;
                        hr = dbDataSource.GetProperty(DBPROPSET_DATASOURCEINFO, DBPROP_DATASOURCENAME, &var);
                        if (FAILED(hr) || (var.vt == VT_EMPTY))
                        {
                              cout<<DAM<<": No Data Source Name Specified."<<endl;
                        }
                        else
                        {
                              cout<<DAM<<": Successfully connected to database. Data source name:\n  "<<COLE2T(var.bstrVal)<<endl;
                       
                              // Prepare SQL query.
                              
                  //            LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,ParentTable.a,ParentTable.b,ParentTable.c FROM ParentTable;";
                   //      LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName, ChildTable.ChildId, ChildTable.ChildName FROM ParentTable INNER JOIN ChildTable ON ParentTable.ParentId = ChildTable.ParentId;";
                              
                              LPCOLESTR query = L"SELECT * from bothtaables;";
                              
                              cout<<DAM<<": SQL query:\n  "<<OLE2T(query)<<endl;

                              // Excecute the query and create a record set.
                              CCommand<CDynamicStringAccessor> cmd;
                              hr = cmd.Open(dbSession, query);
                              DBORDINAL colCount = cmd.GetColumnCount();
                              if (SUCCEEDED(hr) && 0 < colCount)
                              {
                                    cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
                                    DBORDINAL cColumns;
                                    DBCOLUMNINFO* rgInfo = NULL;
                                    OLECHAR* pStringsBuffer = NULL;
                                    cmd.GetColumnInfo(&cColumns, &rgInfo, &pStringsBuffer);
                                    for (int col=0; col < (int)colCount; col++)
                                    {
                                          cout<<" | "<<OLE2T(rgInfo[col].pwszName);
                                    }
                                    cout<<endl;

                                    cout<<DAM<<": Fetch the actual data: "<<endl;
                                    int rowCount = 0;
                                    CRowset<CDynamicStringAccessor>* pRS = (CRowset<CDynamicStringAccessor>*)&cmd;
                                    // Loop through the rows in the result set.
                                    
                                    while (pRS->MoveNext() == S_OK)
                                    {
                                          //*******************************************************************************************************
                                          // THIS IS WERE THE DATA IS DRAWN OUT OF THE TABLE AND EXPORTED OUT
                                          // CONSTRUCT ARRAY
                                          
                                          for (int col=1; col <= (int)colCount; col++)
                                          {
                                                CHAR* szValue = "";
                                                szValue = cmd.GetString(col);
                                                cout<<" | "<<szValue;
                                                // PLACE IN HERE THE ARRAY TO COLLECT ALL THE DATA
                                    
                                                DataArray[rowCount][col] = "";
                                                DataArray[rowCount][col] = szValue;
                  
                                          }
                                          
                                          cout<<endl;
                                          rowCount++;
                                    }
                                    cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
                              }                  
                              else
                              {
                              cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
                              }
                        }  
                  }
            }

            dbDataSource.Close();
            dbSession.Close();
            cout<<DAM<<": Cleanup. Done."<<endl;

      return "AccounFnd";
      //CoUnitialize();
}
dagtbase.png
vsproject.png
SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
Avatar of sydneyguy

ASKER

thanks for the comments sorry i have not closed the question off earlier but was on holidays