I'm trying to write a CIN to connect to a mySQL database, retreive some information, and return it to labVIEW for display. The connection works perfectly using ODBC, but I'm having major trouble getting the information back to LabVIEW. I'm new to CINs and I'm still slightly confused as to how the different structures work. I want to return the data in an array of clusters (using struct names, a 'Set' of 'Records'). LV generated the structs, and I simply renamed some of the fields/names. The code I have so far works up to the point specified in the source below, when I try to initialize the array for data entry. I think what's throwing me off is the conplexity of my structures. Since it's an array of clusters, and not an array of say strings or integers, I'm getting confused. If anyone could help me out by telling me what's wrong with my code, and/or filling in the section of the while loop I'm rather clueless on.
typedef struct {
LStrHandle Number;
LStrHandle SerialNumber;
} Record;
typedef struct {
int32 dimSize;
Record ptr[1];
} Set;
typedef Set **SetHdl;
MgErr CINRun(COpt *ConnectionOptions, LStrHandle *Number, SetHdl *RecordSet);
MgErr CINRun(COpt *ConnectionOptions, LStrHandle *Number, SetHdl *RecordSet)
{
// LV error code
MgErr err = noErr;
// ODBC environment variables
HENV env = NULL;
HDBC dbc = NULL;
HSTMT stmt = NULL;
// Connection options data variables
UCHAR* dsn = malloc(SQL_MAX_DSN_LENGTH * sizeof(UCHAR));
UCHAR* user = malloc(32 * sizeof(UCHAR));
UCHAR* pass = malloc(32 * sizeof(UCHAR));
UCHAR* num = malloc(16 * sizeof(UCHAR));
// Query variables
INT qlen;
INT nlen;
UCHAR colNumber[5];
SDWORD colNumberSize;
UCHAR* query;
INT numRows;
// ODBC return code storage
RETCODE retcode;
/** Prepare data from LV for C++ manipulation **/
strcpy(dsn, LStrBuf((LStrPtr)(*(ConnectionOptions->DSN))));
dsn[(*(ConnectionOptions->DSN))->cnt] = '\0';
strcpy(user, LStrBuf((LStrPtr)(*(ConnectionOptions->Username))));
user[(*(ConnectionOptions->Username))->cnt] = '\0';
strcpy(pass, LStrBuf((LStrPtr)(*(ConnectionOptions->Password))));
pass[(*(ConnectionOptions->Password))->cnt] = '\0';
strcpy(num, LStrBuf((LStrPtr)(*Number)));
// Program crashes here too, but that's the least of my concerns right now. Keep reading down.
//num[(**Number)->cnt] = '\0';
qlen = (int)strlen(
"SELECT m.Number FROM master AS m WHERE (m.Number LIKE '');"
);
nlen = (int)strlen(num);
query = malloc((qlen + nlen + 1) * sizeof(UCHAR));
sprintf(query,
"SELECT m.Number FROM master AS m WHERE (m.Number LIKE '%s'\0);",
num);
// Prepare and make connection to database
SQLAllocEnv (&env);
SQLAllocConnect(env, &dbc);
retcode = SQLConnect(dbc, dsn, SQL_NTS, user, SQL_NTS, pass, SQL_NTS);
// Test success of connection
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLAllocStmt(dbc, stmt);
retcode = SQLPrepare(stmt, query, sizeof(query));
retcode = SQLExecute(stmt);
SQLBindCol(stmt, 1, SQL_C_CHAR, colNumber, sizeof(colNumber), &colNumberSize);
SQLRowCount(stmt, &numRows);
//Program crashes on the following line. I get an error in LV saying something about an error in memory.cpp
DSSetHandleSize((*RecordSet), sizeof(int32) + (numRows * sizeof(Record)));
(**RecordSet)->dimSize = numRows;
retcode = SQLFetch(stmt);
numRows = 0;
while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
/* Need code here to retreive/create Records and put them in the array */
retcode = SQLFetch(stmt);
numRows++;
}
SQLDisconnect(dbc);
}
else
{
}
// Free ODBC environment variables
SQLFreeConnect(dbc);
SQLFreeEnv(env);
// Return LV error code
return err;
}