Hello Adam,
if you want to fetch more than 1 row, you have to allocate the space for that row:
char ** resultSet = (char **) malloc(sizeof(char *)*(numOfColumns+1));
for(int i = 1; i <= numOfColumns; i++)
{
int colLength = meta->getColumnLength(i) + 1;
resultSet[i] = (char *) malloc(sizeof(char) * colLength * numberOfRows);
// sizeof(char), not sizeof(char *) as in your example, also
// space for enough characters for all rows
rc = result->bindColumn(i, SQLDBC_HOSTTYPE_ASCII,
resultSet[i], NULL,
colLength, true);
}
result->setRowSetSize(numberOfRows);
// set the number of rows to retrieve at once
while(result->next() == SQLDBC_OK)
{
rowSet = result->getRowSet();
rc = rowSet->fetch();
for(int n=0; n< rowSet->getRowsAffected(); ++n) {
// getRowsAffected returns the number of fetched rows, which may
// be smaller than the rowset size at the end of the result set
for(i = 1; i <= numOfColumns; i++)
{
printf("%s\n", resultSet[i] + (n * (meta->getColumnLength(i) + 1)));
// data is stored at 0, columnLength, columnLength*2 ...
}
}
}
> -----Original Message-----
> From: Adam Wendt [mailto:adam@stripped]
> Sent: Friday, October 01, 2004 9:16 PM
> To: maxdb@stripped
> Subject: Fetch Multiple Rows with SQLDBC
>
> Hi,
>
> I'm trying to fetch more than 1 row at a time with SQLDBC and
> I'm having
> troubles either allocating enough memory or problems
> accessing more than
> just the first row of each fetch. First I'll give a little example of
> the allocation and access of 1 row then maybe someone can
> show me how it
> needs to be altered for more than 1 row.
>
> // not a real working example but basically what i'm doing that works
> for 1 row
> char ** resultSet = (char **) malloc(sizeof(char *)*(numOfColumns+1));
> for(int i = 1; i <= numOfColumns; i++)
> {
> int colLength = meta->getColumnLength(i) + 1;
> resultSet[i] = (char *) malloc(sizeof(char *)*colLength);
> rc = result->bindColumn(i, SQLDBC_HOSTTYPE_ASCII,
> resultSet[i], NULL,
> colLength, true);
> }
>
> while(result->next() == SQLDBC_OK)
> {
> rowSet = result->getRowSet();
> rc = rowSet->fetch();
> for(i = 1; i <= numOfColumns; i++)
> {
> printf("%s\n", resultSet[i]);
> }
> }
>
> That isn't a full code listing but it shows what I'm doing
> and it works
> when you do result->setRowSetSize(1); but its not setup for
> having the
> row set size more than 1, so I'm looking for suggestions on
> how to deal
> with allocating the memory and accessing with a larger setRowSetSize.
>
> --
> MaxDB Discussion Mailing List
> For list archives: http://lists.mysql.com/maxdb
> To unsubscribe:
> http://lists.mysql.com/maxdb?unsub=1
>