The MySQL (nonstandard extension) integer-type display width may result in allocating a buffer too small to handle integer column values that exist in rows. Therefore, rather than rely on the MYSQL_FIELD length member (which describes display width), use MAX_BIGINT_WIDTH for all integer columns. This approach ensures that we don't cause invalid data truncation, and does not introduce the efficiency problems that would be caused by the client-side data buffering that results from the use of max_length (which is only available when specifying the STMT_ATTR_UPDATE_MAX_LENGTH option with mysql_stmt_attr_set() followed by a call to mysql_stmt_store_result() ). The attached patch adds a function, qIsInteger() (similar to qIsBlob() ), and sets the value of fieldInfo->length to MAX_BIGINT_WIDTH for integer columns ( http://dev.mysql.com/doc/refman/5.0/en/c-api-prepared-statement-datatypes.html ). I felt the code complexity for handling each integer type separately was not warranted for the small amount of space savings involved (MAX_BIGINT_WIDTH is 20, MAX_INT_WIDTH is 10, MAX_MEDIUMINT_WIDTH is 8, MAX_SMALLINT_WIDTH is 5, and MAX_TINYINT_WIDTH is 3 --and all but MAX_BIGINT_WIDTH and MAX_MEDIUMINT_WIDTH require the + 1 (already done in the existing code) for the sign, unless unsigned). MAX_BIGINT_WIDTH was added to MySQL 4.1.5 in response to MySQL bug #4788 ( http://bugs.mysql.com/bug.php?id=4788 and http://dev.mysql.com/doc/refman/4.1/en/news-4-1-5.html ), but MAX_BIGINT_WIDTH and all the other code added by this patch are only used in a section of code already protected by "#if MYSQL_VERSION_ID >= 40108" (since prepared statements are only used for MySQL 4.1.8 and above). MAX_BIGINT_WIDTH is defined in mysql_com.h, which is included by mysql.h, which is included by qsql_mysql.h. --- Index: qt-x11-opensource-src-4.5.3/src/sql/drivers/mysql/qsql_mysql.cpp =================================================================== --- qt-x11-opensource-src-4.5.3.orig/src/sql/drivers/mysql/qsql_mysql.cpp 2009-12-03 01:21:13.000000000 -0500 +++ qt-x11-opensource-src-4.5.3/src/sql/drivers/mysql/qsql_mysql.cpp 2009-12-03 03:19:03.000000000 -0500 @@ -315,6 +315,15 @@ || t == MYSQL_TYPE_LONG_BLOB; } +static bool qIsInteger(int t) +{ + return t == MYSQL_TYPE_TINY + || t == MYSQL_TYPE_SHORT + || t == MYSQL_TYPE_LONG + || t == MYSQL_TYPE_LONGLONG + || t == MYSQL_TYPE_INT24; +} + void QMYSQLResultPrivate::bindBlobs() { int i; @@ -362,6 +371,13 @@ fieldInfo->length = 0; hasBlobs = true; } else { + // fieldInfo->length specifies the display width, which may be too + // small to hold valid integer values (see + // http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html ), so + // always use the MAX_BIGINT_WIDTH for integer types + if (qIsInteger(fieldInfo->type)) { + fieldInfo->length = MAX_BIGINT_WIDTH; + } fieldInfo->type = MYSQL_TYPE_STRING; } bind = &inBinds[i];