SQLBindFileToCol
SQLBindFileToCol#
SQLBindFileToCol binds a file or files to the columns in the result set for BLOB or CLOB data type.
Syntax#
SQLRETURN SQLBindFileToCol(
SQLHSTMT stmt,
SQLSMALLINT col,
SQLCHAR * fileName,
SQLLEN * fileNameLength,
SQLUINTEGER * fileOptions,
SQLLEN fileNameBufferSize,
SQLLEN * valueLength);
Arguments#
Data Type | Argument | In/Output | Description |
---|---|---|---|
SQLHSTMT | stmt | Input | An instruction handle for the found results. |
SQLSMALLINT | col | Input | Begins from #1 in the order of columns in the result set to bind. |
SQLCHAR * | filename | Input (Pending) | A pointer to the buffer that holds a filename or an array of filenames. It cannot be NULL. Upon SQLFetch(), there should be a filename stored in this buffer, and SQLFetch() returns data to the file(s). Either of an absolute path (recommended) and a relative path is allowed. |
SQLLEN * | fileNameLength | Input (Pending) | A pointer to the buffer that holds a filename length or an array of filename lengths. Upon SQLFetch(), there should be a filename length stored in this buffer. If this argument is NULL, a filename is regarded as a null-terminated string. That is, it has the same effect as if SQL_NTS were stored in the memory pointed by this argument. The max length of a filename is 255 characters. |
SQLUINTEGER * | fileOptions | Input (Pending) | A pointer to the buffer that holds a file option or an array of file options. Upon SQLFetch(), there should a file option stored in this buffer. The following options are available: SQL_FILE_CREATE creates one if there is no file, and returns SQL_ERROR if there is a file. SQL_FILE_OVERWRITE creates one if there is no file, and overwrites it if there is a file. SQL_FILE_APPEND creates one if there is no file, and appends to it if there is a file. Only one of the above options can be selected and there is no default option. This argument cannot be NULL. |
SQLLEN | fileNameBufferSize | Input | Sets the length of the fileName buffer. |
SQLLEN * | valueLength | Output(Pending) | A pointer to the buffer that holds an indicator variable or an array of indicator variables. It cannot be NULL. It is used to return the length of the data stored in a file or to indicate that LOB is NULL. SQLFetch() can return the following values to the buffer pointed by this pointer: 1. Data length, 2. SQL_NULL_DATA. |
Return Values#
SQL_SUCCESS
SQL_SUCCESS_WITH_INFO
SQL_INVALID_HANDLE
SQL_ERROR
Description#
SQLBindFileToCol() binds LOB data in the result set to a file, and SQLBindCol() binds it to an application variable (memory buffer).
If SQLFetch() is called after SQLBindFileToCol() is called, LOB data from DBMS is stored in a file, and the length (byte) of the data stored in the file is stored in the buffer pointed by the valueLength pointer. If LOB is NULL, SQL_NULL_DATA is stored in the buffer pointed by the valueLength pointer. The values of fileName, fileNameLength and fileOptions arguments are referred to upon SQLFetch(), and any error in these arguments is also reported during fetching.
To transfer more than one LOB to a file at once upon fetching, all of the fileName, fileNameLength, fileOptions and valueLength arguments should be arrays.
Diagnosis#
SQLSTATE | Description | Comments |
---|---|---|
08S01 | Communication line fault (Data transmission failure) | Communication line fails before function processing is complete between Altibase CLI driver and DB. |
HY000 | General error |
Related Functions#
SQLBindCol
SQLBindFileToParam
SQLDescribeCol
SQLFetch
Examples#
It is assumed that a table has been created with the following DDL.
CREATE TABLE T1 (i1 INTEGER PRIMARY KEY, i2 BLOB);
Write one LOB to a file.#
SQLCHAR fileName[16];
SQLLEN fileNameLength = 15;
SQLUINTEGER fileOptions = SQL_FILE_CREATE;
SQLLEN valueLength;
.
strcpy(query, "SELECT i2 FROM T1 WHERE i1=1");
if (SQLPrepare(stmt, query, SQL_NTS) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLPrepare : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
/* Specify a file to put the result values of Select. */
strcpy(fileName, "Terminator2.avi");
if (SQLBindFileToCol(stmt, 1, fileName, &fileNameLength, &fileOptions, 16, &valueLength) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLBindFileToCol : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLExecute(stmt) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLExecute : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLFetch(stmt) == SQL_SUCCESS)
{
printf("SQLFetch success!!!\n");
}
else
{
execute_err(dbc, stmt, "SQLFetch : ");
}
Write three LOB's to a file#
SQLCHAR fileName[3][10];
SQLLEN fileNameLength[3];
SQLUINTEGER fileOptions[3];
SQLLEN valueLength[3];
.
.
.
if (SQLSetStmtAttr(stmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) 3, 0) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLSetStmtAttr : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLSetStmtAttr(stmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLSetStmtAttr : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
strcpy(query, "SELECT i2 FROM T1 WHERE i1 >= 1 AND i1 <= 3");
if (SQLExecDirect(stmt, query, SQL_NTS) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLExecDirect : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
/* Specify a file to put the result values of Select. */
strcpy(fileName[0], "Cube.avi");
strcpy(fileName[1], "Movie.avi");
strcpy(fileName[2], "Term.avi");
for (i = 0; i < 3; i++)
{
fileNameLength[i] = strlen(fileName[i]);
fileOptions[i] = SQL_FILE_CREATE;
}
if (SQLBindFileToCol(stmt, 1, (SQLCHAR *) fileName, fileNameLength, fileOptions, 10, valueLength) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLBindFileToCol : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLFetch(stmt) == SQL_SUCCESS)
{
printf("SQLFetch success!!!\n");
}
else
{
execute_err(dbc, stmt, "SQLFetch : ");
Write n LOB's to a file#
SQLCHAR fileName[11];
SQLLEN fileNameLength = 10;
SQLUINTEGER fileOptions = SQL_FILE_OVERWRITE;
SQLLEN valueLength;
.
strcpy(query, "SELECT i2 FROM T1");
if (SQLExecDirect(stmt, query, SQL_NTS) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLExecDirect : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
if (SQLBindFileToCol(stmt, 1, fileName, &fileNameLength, &fileOptions, 11, &valueLength) != SQL_SUCCESS)
{
execute_err(dbc, stmt, "SQLBindFileToCol : ");
SQLFreeStmt(stmt, SQL_DROP);
return SQL_ERROR;
}
for (i = 0; ; i++)
{
sprintf(fileName, "Term%02d.avi", i + 1);
rc = SQLFetch(stmt);
if (rc == SQL_SUCCESS)
{
printf("SQLFetch of file[%02] success!!!\n", i + 1);
}
else if (rc == SQL_NO_DATA)
{
break;
}
else
{
execute_err(dbc, stmt, "SQLFetch : ");
break;
}
}