0

我想将 csv 文件的内容传输到 mysql。在我的 csv 文件中,有些列的文本包含逗号。

我正在使用下面的代码来传输内容

`

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import au.com.bytecode.opencsv.CSVReader;




public class CSVLoader {


    static int  count;
    private static final 
        String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
    private static final String TABLE_REGEX = "\\$\\{table\\}";
    private static final String KEYS_REGEX = "\\$\\{keys\\}";
    private static final String VALUES_REGEX = "\\$\\{values\\}";

    private Connection connection;
    private char seprator;

    /**
     * Public constructor to build CSVLoader object with
     * Connection details. The connection is closed on success
     * or failure.
     * @param connection
     */
    public CSVLoader(Connection connection) {
        this.connection = connection;
        //Set default separator
        this.seprator = ',';
    }

    /**
     * Parse CSV file using OpenCSV library and load in 
     * given database table. 
     * @param csvFile Input CSV file
     * @param tableName Database table name to import data
     * @param truncateBeforeLoad Truncate the table before inserting 
     *          new records.
     * @throws Exception
     */
    public void loadCSV(String csvFile, String tableName,
            boolean truncateBeforeLoad) throws Exception {

        CSVReader csvReader = null;
        if(null == this.connection) {
            throw new Exception("Not a valid connection.");
        }
        try {

            csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("Error occured while executing file. "
                    + e.getMessage());
        }

        //String[] headerRow = csvReader.readNext();
        String[] headerRow = csvReader.readNext();
        count++;
        if (null == headerRow) {
            throw new FileNotFoundException(
                    "No columns defined in given CSV file." +
                    "Please check the CSV file format.");
        }

        String questionmarks = StringUtils.repeat("?,", headerRow.length);
        System.out.println(headerRow.length);
        questionmarks = (String) questionmarks.subSequence(0, questionmarks
                .length() - 1);

        String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
        query = query
                .replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
        query = query.replaceFirst(VALUES_REGEX, questionmarks);

        System.out.println("Query: " + query);

        String[] nextLine;
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.connection;
            con.setAutoCommit(false);
            ps = con.prepareStatement(query);

            if(truncateBeforeLoad) {
                //delete data from table before loading csv
                con.createStatement().execute("DELETE FROM " + tableName);
            }

            final int batchSize = 1000;
            int count = 0;
            Date date = null;
            while ((nextLine = csvReader.readNext()) != null) {

                if (null != nextLine) {
                    int index = 1;
                    for (String string : nextLine) {
                        date = DateUtil.convertToDate(string);
                        if (null != date) {
                            ps.setDate(index++, new java.sql.Date(date
                                    .getTime()));
                        } else {
                            ps.setString(index++, string);
                        }
                    }
                    System.out.println(count);
                    ps.addBatch();
                    System.out.println(count);
                }
                if (++count % batchSize == 0) {
                    System.out.println(count);
                    ps.executeBatch();
                }
            }
            ps.executeBatch(); // insert remaining records
            con.commit();
        } catch (Exception e) {
            con.rollback();
            e.printStackTrace();
            throw new Exception(
                    "Error occured while loading data from file to database."
                            + e.getMessage());
        } finally {
            if (null != ps)
                ps.close();
            if (null != con)
                con.close();

            csvReader.close();
        }
    }

    public char getSeprator() {
        return seprator;
    }

    public void setSeprator(char seprator) {
        this.seprator = seprator;
    }

}

` 执行它时,我收到“没有为参数 23 指定值”的错误。我的数据库表有 22 列,csv 文件也有 22 列。所以我猜测在第一行本身有一个文本,其中有一个逗号,它无法解析它,因此它假设为 23列而不是 22。任何人都可以帮助我澄清问题并为我提供解决方案。

4

2 回答 2

0

我相信最直接的问题是在将列名插入 SQL 语句时不要转义它们。您正在创建的是这种形式的声明:

INSERT INTO sometable(key1,key2,key3) VALUES(?,?,?)

现在,如果您在标题行中有一个逗号(假设一个键是“ke,y3”),即使您的 CSV 库正确读取它,您也将创建如下内容:

INSERT INTO sometable(key1,key2,ke,y3) VALUES(?,?,?)

现在您的值数和列数不匹配。请注意,这也可能发生在其他一些字符上:也许您在一个键中有一个问号,它被解释为参数占位符?

解决方案:为了省去一些麻烦,尽可能避免在键中使用这些字符。我不确定mysql如何以及是否会正确处理它们,但如果确实如此,您至少需要在插入它们之前转义列名。我不确定您将如何正确安全地执行此操作(以防止 SQL 注入),但由于这显然是一种一次性工具,因此将列名包装在反引号中应该足够好:

INSERT INTO sometable(`key1`,`key2`,`ke,y3`) VALUES(?,?,?)
于 2013-11-04T23:18:39.470 回答
-1

CSV 文件中有两种逗号。一种逗号分隔字段,另一种逗号是文本的一部分,总是出现在引号之间。您需要解析引号外的逗号与引号内的逗号不同。您的代码似乎没有这样做。也许是这样的:

repeat
  c <-read next character
  if (c == '"')
    parse quoted field  // May include commas.
  else
    parse non-quoted field // Will not include commas.
  endif
until file all read.

使用不同的方法来解析带引号和不带引号的字段可以很容易地正确处理这两种类型的逗号。

于 2013-11-04T23:04:11.063 回答