简体   繁体   English

Java:将 CSV 值导入 SQL 服务器十进制字段时出现问题

[英]Java: Trouble with importing CSV values into a SQL Server DECIMAL field

I'm using SQLServerBulkCopy to import CSV files into a database.我正在使用 SQLServerBulkCopy 将 CSV 文件导入数据库。 The code is:代码是:

    private void bulkCopy(String tableName, String csvFileName) throws Exception {
    Integer rowCount = 0;        
    Instant copyStart = Instant.now();

    try {
        Integer column = 0;
        SQLServerBulkCopy copier = new SQLServerBulkCopy(dbConnection);
        SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord(csvFileName, true);
        Statement statement = dbConnection.createStatement();

        // Set the metadata for the column to be copied, as well as mapping the columns.
        for (String thisColumn : commonColumns) {
            column += 1;
            Integer index = tableData.columns.indexOf(thisColumn);
            Integer size = tableData.sizes.get(index);
            Integer type = getType(tableData.types.get(index));
            
            fileRecord.addColumnMetadata(index + 1, thisColumn, type, size, 0);
            copier.addColumnMapping(thisColumn, thisColumn);
        }
        // System.exit(0);

        copier.setDestinationTableName(tableName);
        copier.writeToServer(fileRecord);
        rowCount = getRowCount(statement, tableName);
        copier.close();
    }
    catch (SQLException e){
        throw e;
    }
    Instant copyEnd = Instant.now();
    Duration elapsed = Duration.between(copyStart, copyEnd);
    logger.info("DEBUG: file is " + csvFileName);

    logger.info("It took " + utils.formatDuration(elapsed) + " to write " + rowCount + " rows to the " + tableName + " table.");
}

The problem is a DECIMAL(16,2) field with a 100 in the CSV file.问题是 CSV 文件中的DECIMAL(16,2)字段为 100。 When I try to run my code, I get this error:当我尝试运行我的代码时,我收到此错误:

com.microsoft.sqlserver.jdbc.SQLServerException: An error occurred while converting the '"100"' value to JDBC data type DECIMAL. com.microsoft.sqlserver.jdbc.SQLServerException:将“100”值转换为 JDBC 数据类型 DECIMAL 时出错。

I have no idea how to keep this error from happening.我不知道如何防止此错误发生。

I think the problem is that your number is not a number in your file.我认为问题在于您的号码不是文件中的号码。 It is a String with the value 100.它是一个值为 100 的字符串。

Can you change the CSV File to have 100 instead of "100" or better can you activate the escaping of field delimters in this SQLServerBulkCopy Object?您能否将 CSV 文件更改为 100 而不是“100”或更好您可以激活此 SQLServerBulkCopy Z497031794414A552444BZF90151AC3B 中的字段分隔符 escaping 吗? Is it this?是这个吗? https://github.com/microsoft/mssql-jdbc/blob/17569c6aa1347d1c691581055c4f7a98fc0ab813/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCSVFileRecord.java#L555 https://github.com/microsoft/mssql-jdbc/blob/17569c6aa1347d1c691581055c4f7a98fc0ab813/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCSVFileRecord.java#L555

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM