1

i am making a program where I would read data from txt files and store them in tables in mysql. In my program I am making the table with the fields that I would like to have and then with the command of "Load Data Infile" I am inserting the values in the table.

The files that I have contains a column (named as litres)
enter image description here
with data that are float numbers such as

0,234

12,234

3,004 etc.

At the beginning of my program when i create the table, the litres field is described as FLOAT. But when i ran my program I get an SQLException : Data truncated for column 'litres' at row 1.

The code for creating the table and inserting the values are the below:

private static String getCreateTable1(Connection con, String tablename) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Statement stmt = con.createStatement();
            String createtable = "CREATE TABLE " + tablename
                    + " ( text VARCHAR(255), price INT , day VARCHAR(255), litres FLOAT )";
            System.out.println("Create a new table in the database");
            stmt.executeUpdate(createtable);
        } catch (Exception e) {
            System.out.println(((SQLException) e).getSQLState());
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        return null;
    }

    private static String importData(Connection con, File txtFile,
            String tablename) {

        try {
            Statement stmt;

            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            String path = txtFile.getAbsolutePath();
            String importingdata = "LOAD DATA INFILE '"
                    + path.replace('\\', '/')
                    + "' INTO TABLE " + tablename
                    + " FIELDS TERMINATED BY '\t'";
            System.out.println("fill the table");
            stmt.executeUpdate(importingdata);

        } catch (Exception e) {
            System.out.println(((SQLException) e).getSQLState());
            System.out.println(e.getMessage());
            e.printStackTrace();

        }
        return null;
    }

How can i fix it? Could anyone help me? I think that the problem is the comma? How can i make it recognize it?

infused
  • 24,000
  • 13
  • 68
  • 78
dedmar
  • 401
  • 3
  • 12
  • 22

3 Answers3

1

Parsing decimal numbers is locale-specific. You need to change your statement to substitute the comma character ',' with a dot character '.' before giving it to MySQL. One way to do it is adding a cast to your LOAD DATA INFILE statement:

LOAD DATA INFILE 'myfile.txt'
INTO TABLE tablename
FIELDS TERMINATED BY '\t'
(COL1, @litres, COL3, ...) -- Note the @ in front of the float column
SET litres = REPLACE(@litres, ',', '.')

You need to list all the columns that you are importing. The columns which you import "as is" need to be listed without an "at" sign @; the columns where you must replace commas with dots need to be listed with an "at" sign @. For each float column you need to add a replace call on the last line.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • OP mentioned in comment *its not separated numbers with commas. Is float numbers.* – Ajinkya Jun 17 '13 at 10:08
  • @Karna That's precisely what this solution expects: fields are delimited by tabs; commas remain a part of the float, enabling the call of `REPLACE` to succeed. – Sergey Kalinichenko Jun 17 '13 at 10:09
0

This exception means values you are inserting into litres column are bigger than the column can accommodate. Looking at the values you mentioned seems like your using liters as a varchar, check if you have the size limit on it.
Update your table to allow desired values.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

It might be that there are empty records in your file, that is throwing the error message.

See similar issue here: Getting a mysql Error Code: 1265. Data truncated for column when doing a LOAD DATA LOCAL INFILE

Community
  • 1
  • 1
Minesh
  • 2,284
  • 1
  • 14
  • 22