LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   DB2 VARCHAR type usage with NULL character (https://www.linuxquestions.org/questions/programming-9/db2-varchar-type-usage-with-null-character-867066/)

The Oate 03-07-2011 05:01 PM

DB2 VARCHAR type usage with NULL character
 
I have a vector of bytes that I need to store in a DB2 database and was wanting to do it with a column declared as VARCHAR () FOR BIT DATA. I can do this with no problem except when a byte representing the NULL character occurs in the vector. Anytime this occurs, the rest of the bytes are truncated. I know this is the proper behavior for if this were a string, but I was under the impression that if the column is declared as I previously mentioned, that this would support any variable length binary data including the NULL character.

I've found repeated references to people dealing with DB2 tables where the data in VARCHAR columns contained leading and embedded NULLS, so I know this is possible. Can anyone provide me with the proper INSERT statement in order to get this to work? This is the current BindParameters call I'm making for the VARCHAR () FOR BIT DATA column that's truncating after the NULL character occurs.

Code:

SQLBindParameter(handleStmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR,
        SQL_VARCHAR, (SQLUINTEGER)(30000), 0, data2, 30000,
        &sqlNts);


The Oate 03-08-2011 12:11 PM

I've changed the field in the table to now be declared as a BLOB(30000) and I'm using the below bind parameter statement with this, however I'm still seeing the same problem. When the first byte is reached that contains 0 (NULL character), the rest of what's in ptr is ignored. ptr is declared as unsigned char ptr[30000]. For testing purposes, I've just been using a for loop and storing the value of the counter % 255 + 1 so that all the characters except for NULL are represented. Then I've gone in and just inserted a 0 value in a few places. Anyone know how I can get the full contents of ptr to be stored?

Code:

SQLBindParameter(handleStmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY,
                    SQL_BLOB, (SQLUINTEGER)30000, 0, ptr, (SQLINTEGER)30000, &sqlNts);

Also, here is the format of the INSERT statement that I'm trying to use:

Code:

char *insertStmt =
        "INSERT INTO TESTTABLE (CATEGORY, TOPIC, DATA) VALUES(?, ?, CAST(? AS BLOB(30000)))";



All times are GMT -5. The time now is 02:38 AM.