sydneyguy
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","89 9" instead of "loftus,"water","","899"
this is the section of code doing most of the work
CRowset<CDynamicStringAcce ssor>* pRS = (CRowset<CDynamicStringAcc essor>*)&c md;
// 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(voi d)
{
}
accessMSdb::~accessMSdb(vo id)
{
}
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.O LEDB.4.0;D ata Source=C:\mydatabase.mdb;U ser Id=admin;Password=;
// Provider=Microsoft.Jet.OLE DB.4.0;Dat a Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;Us er Id=admin;Password=;
LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.O LEDB.4.0;D ata Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;Us er 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.OpenFromIniti alizationS tring(lpcO leConnect) ;
if (FAILED(hr))
{
cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<e ndl;
}
else
{
hr = dbSession.Open(dbDataSourc e);
if (FAILED(hr))
{
cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<e ndl;
}
else
{
CComVariant var;
hr = dbDataSource.GetProperty(D BPROPSET_D ATASOURCEI NFO, 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)<<en dl;
// Prepare SQL query.
// LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,Par entTable.a ,ParentTab le.b,Paren tTable.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<CDynamicStringAcc essor> 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(&cColumn s, &rgInfo, &pStringsBuffer);
for (int col=0; col < (int)colCount; col++)
{
cout<<" | "<<OLE2T(rgInfo[col].pwszN ame);
}
cout<<endl;
cout<<DAM<<": Fetch the actual data: "<<endl;
int rowCount = 0;
CRowset<CDynamicStringAcce ssor>* pRS = (CRowset<CDynamicStringAcc essor>*)&c md;
// 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
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","89
this is the section of code doing most of the work
CRowset<CDynamicStringAcce
// 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(voi
{
}
accessMSdb::~accessMSdb(vo
{
}
string accessMSdb::ExtractDataDB(
{
// --------------------------
// 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(
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.O
// Provider=Microsoft.Jet.OLE
LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.O
// 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.OpenFromIniti
if (FAILED(hr))
{
cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<e
}
else
{
hr = dbSession.Open(dbDataSourc
if (FAILED(hr))
{
cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<e
}
else
{
CComVariant var;
hr = dbDataSource.GetProperty(D
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)<<en
// Prepare SQL query.
// LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,Par
// 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<CDynamicStringAcc
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(&cColumn
for (int col=0; col < (int)colCount; col++)
{
cout<<" | "<<OLE2T(rgInfo[col].pwszN
}
cout<<endl;
cout<<DAM<<": Fetch the actual data: "<<endl;
int rowCount = 0;
CRowset<CDynamicStringAcce
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the comments sorry i have not closed the question off earlier but was on holidays
Not enough information to confirm an answer.