Jeremy Witmer
Jeremy Witmer

Reputation:

How to fix MySQL Data Truncation Error when inserting a lot of data?

I'm working with a fairly simple database, from a Java application. We're trying to insert about 200k of text at a time, using the standard JDBC mysql adapter. We intermittently get a com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column error.

The column type is longtext, and database collation is UTF-8. The error shows up using both MyISAM and InnoDB table engines. Max packet size has been set ot 1 GB on both client and server sides, so that shouldn't be causing a problem, either.

Upvotes: 13

Views: 50605

Answers (5)

grigson
grigson

Reputation: 3766

In mysql you can use MEDIUMTEXT or LONGTEXT field type for large text data

Upvotes: 4

Szemere
Szemere

Reputation: 188

I just hit this problem and solved it by removing all the non-standard ascii characters in my text (following the UTF-8 advice above).

I had the problem on a Debian 4, Java 5 system; but the same code worked fine with Ubuntu 9.04, Java 6. Both run MySql 5.

Upvotes: 1

Avi
Avi

Reputation: 20152

Check that your UTF-8 data is all 3-byte Unicode. If you have 4-byte characters (legal in Unicode and Java, illegal in MySQL 5), it can throw this error when you try to insert them. This is an issue that should be fixed in MySQL 6.0.

Upvotes: 11

Aaron
Aaron

Reputation: 2406

It sounds to me like you are trying to put too many bytes into a column. I ran across a very similar error with MySQL last night due to a bug in my code. I meant to do

foo.status = 'inactive'

but had actually typed

foo.state = 'inactive'

Where foo.state is supposed to be a two character code for a US State ( varchar(2) ). I got the same error as you. You might go look for a similar situation in your code.

Upvotes: 2

Nicholas
Nicholas

Reputation: 576

Well you can make it ignore the error by doing an INSERT IGNORE which will just truncate the data and insert it anyway. (from http://dev.mysql.com/doc/refman/5.0/en/insert.html )

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued. Data conversions that would trigger errors abort the statement if IGNORE is not specified. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort.

Upvotes: 5

Related Questions