数据截断:第 1 行“徽标"列的数据太长 [英] Data truncation: Data too long for column 'logo' at row 1

查看:25
本文介绍了数据截断:第 1 行“徽标"列的数据太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将照片插入 MySQL 表的 BLOB 列中,但出现异常:

I am trying to insert a photo into a BLOB column of a MySQL table, and I get an exception:

Data too long for column 'logo' at row 1. 

这是 JDBC:

    int idRestaurant = 42;
    String restoname=  "test";
    String restostatus=  "test";
    InputStream fileContent = getUploadedFile();
    int fileSize = getUploadedFileSize();

    Class.forName("com.mysql.jdbc.Driver");
    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/resto" , "root" , "" )) {
        PreparedStatement ps = conn.prepareStatement("insert into restaurants (idRestaurant, restaurantName, status, logo) values(?,?,?,?)");
        ps.setInt(1, idRestaurant);
        ps.setString(2, restoname);
        ps.setString(3, restostatus);
        ps.setBinaryStream(4, fileContent, fileSize);
        ps.executeUpdate();
        conn.commit();
    }

我该如何解决这个问题?

How do I solve this problem?

推荐答案

您试图插入的数据大于列 logo 所允许的数据.

You are trying to insert data that is larger than allowed for the column logo.

根据需要使用以下数据类型

Use following data types as per your need

TINYBLOB   :     maximum length of 255 bytes  
BLOB       :     maximum length of 65,535 bytes  
MEDIUMBLOB :     maximum length of 16,777,215 bytes  
LONGBLOB   :     maximum length of 4,294,967,295 bytes  

使用 LONGBLOB 来避免这个异常.

Use LONGBLOB to avoid this exception.

这篇关于数据截断:第 1 行“徽标"列的数据太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆