从csv读取到sqlite时出现定界符问题

时间:2018-08-27 14:02:46

标签: android sqlite csv

我尝试了网上的所有搜索,但无法找到解决方案。我已经设法创建了一个csv文件(如图所示)。但是,无论我如何将其导入到sqlite中,我都无法在sqlite中的正确位置中放置正确的行,但我相信这是关于定界符的,但是找不到能帮我的忙,我正在寻找它最近2天。谢谢

File exportDir = new File(Environment.getExternalStorageDirectory(), "/XXX/");
                if (!exportDir.exists()) { exportDir.mkdirs(); }

                File file2 = new File(exportDir, "XXX.csv");
                FileReader file = new FileReader(file2);
                BufferedReader buffer = new BufferedReader(file);
                ContentValues contentValues=new ContentValues();
                String line = "";
                String tableName =WordListOpenHelper.MY_LIST_TABLE; 

                db.beginTransaction();
                while ((line = buffer.readLine()) != null)

                {
                    x++;
                    String[] str = line.split("\\+");  

                   if (x>5) {

                       contentValues.put(Contract.WordList.KEY_ENTRY, str[0]);

                       contentValues.put(Contract.WordList.KEY_NICKNAME_DATE, str[1]);
                      contentValues.put(Contract.WordList.KEY_LOCATION, str[2]);
                       contentValues.put(Contract.WordList.KEY_LOCATION_IMAGE, str[3]);
                       contentValues.put(Contract.WordList.KEY_HEART, str[4]);
                       db.insert(tableName, null, contentValues);
                   }

                }
                db.setTransactionSuccessful();
                db.endTransaction();

和CSVWriter类

 public class CSVWriter {

private PrintWriter pw;
private String separator;
private char escapechar;
private String lineEnd;
private char quotechar;

public static final String DEFAULT_SEPARATOR = "+\n";
public static final char NO_QUOTE_CHARACTER = '\u0000';
public static final char NO_ESCAPE_CHARACTER = '\u0000';
public static final String DEFAULT_LINE_END = "+\n";
public static final char DEFAULT_QUOTE_CHARACTER = '"';
public static final char DEFAULT_ESCAPE_CHARACTER = ' ';


public CSVWriter(Writer writer) {
    this(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,
            DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);
}

public CSVWriter(Writer writer, String separator, char quotechar, char escapechar, String lineEnd) {
    this.pw = new PrintWriter(writer);
    this.separator = separator;
    this.quotechar = quotechar;
    this.escapechar = escapechar;
    this.lineEnd = lineEnd;
}

public void writeNext(String[] nextLine) {
    try{

    if (nextLine == null)
        return;

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nextLine.length; i++) {

        if (i != 0) {
            sb.append(separator);
        }

        String nextElement = nextLine[i];
        if (nextElement == null)
            continue;
        if (quotechar != NO_QUOTE_CHARACTER)
            sb.append(quotechar);
        for (int j = 0; j < nextElement.length(); j++) {
            char nextChar = nextElement.charAt(j);
            if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
                sb.append(escapechar).append(nextChar);
            } else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
                sb.append(escapechar).append(nextChar);
            } else {
                sb.append(nextChar);
            }
        }
        if (quotechar != NO_QUOTE_CHARACTER)
            sb.append(quotechar);
    }
    sb.append(lineEnd);
    pw.write(sb.toString());
}
    catch (Exception ex) {
        ex.printStackTrace();
    }
}
public void close() throws IOException {
    pw.flush();
    pw.close();
}
public void flush() throws IOException {
    pw.flush();
}
}

文件管理器中的CSV文件:

enter image description here

1 个答案:

答案 0 :(得分:0)

这不完全是一个csv文件,因此需要特殊处理。
使用此方法:

public String getLine(String line) {
    line = line.trim();
    if (line.isEmpty())
        return "";
    if (line.equals("\"\"+"))
        return "";

    if (line.length() <= 3)
        return "";

    return line.substring(1, line.length() - 1);
}

,而不是您的while循环:

    boolean noMoreLines = false;

    for (int i = 1; i <= 5; i++) {
        line = buffer.readLine();
        if (line == null) {
            noMoreLines = true;
            break;
        }
    }

    if (!noMoreLines) {
        while ((line = buffer.readLine()) != null) {
            contentValues.put(Contract.WordList.KEY_ENTRY, getLine(line));

            if ((line = buffer.readLine()) != null)
                contentValues.put(Contract.WordList.KEY_NICKNAME_DATE, getLine(line));
            if ((line = buffer.readLine()) != null)
                contentValues.put(Contract.WordList.KEY_LOCATION, getLine(line));
            if ((line = buffer.readLine()) != null)
                contentValues.put(Contract.WordList.KEY_LOCATION_IMAGE, getLine(line));
            if ((line = buffer.readLine()) != null)
                contentValues.put(Contract.WordList.KEY_HEART, getLine(line));

            db.insert(tableName, null, contentValues);
        }
    }