在Java中将.csv导入SQLite

时间:2018-03-27 13:06:21

标签: java sqlite

我必须编写一个程序,它会自动读出我的.csv并将数据放入我的SQLite中。这段代码填满了我之前创建的表格中的第一行。 如何跳过/转到下一行,填写它?

我猜它是循环中的东西,但我不知道该改变什么......

(我跳过第一行,因为它包含列。)

代码:

public static void read()
 {
  String csv = "C:\\Users\\schneider\\Desktop\\Schneider\\Excel\\DienstbarkeitenTabelle.csv";
  BufferedReader br = null;
  String line = "";
  String csvSplitBy = ",";
  int currentLine = 0;
  Connection conn = null;

  try
  {
      conn = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/db/tollerName");
      PreparedStatement prep = conn.prepareStatement("insert into tollerName(Bauprojekt, Bauprojektnummer, Auftragsnummer,Trassierung, JahrSAP, ProjektnummerSPA, Anlagenzahl, AktuelleLeitungslaenge, VorgaengeZumProjekt, VertraegeZumProjekt, Firma, Flurst, Blatt, Verf, Anl, RE, LL, BreiteSS, FlaecheRA, Entsch, Haupteigentuemer, Gemarkung, Grundbuchamt, Strase, Hausnummer, Hausnummerzusatz, Ort, plz, VkWert, prozentBPD, Anlage, GFR, GBA, Empfaenger, Bank, IBAN, BIC, von, an, JahrDerVergabe, Zahlungsgrund, Netto, Aufwand, Umsatzsteuer) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

      br = new BufferedReader(new FileReader(csv));
      while((line = br.readLine()) != null)
      {
          if(currentLine > 0)
          {
              String[] table = line.split(csvSplitBy);
              prep.setString(1, (table[0]));
              prep.setString(2, (table[1]));
              prep.setString(3, (table[2]));
              prep.setString(4, (table[3]));
              prep.setString(5, (table[4]));
              prep.setString(6, (table[5]));
              prep.setInt(7, Integer.parseInt(table[6]));
              prep.setDouble(8, Double.parseDouble(table[7]));
              prep.setInt(9, Integer.parseInt(table[8]));
              prep.setInt(10, Integer.parseInt(table[9]));
              prep.setString(11, (table[10]));
              prep.setString(12, (table[11]));
              prep.setString(13, (table[12]));
              prep.setString(14, (table[13]));
              prep.setString(15, (table[14]));
              prep.setString(16, (table[15]));
              prep.setDouble(17, Double.parseDouble(table[16]));
              prep.setDouble(18, Double.parseDouble(table[17]));
              prep.setDouble(19, Double.parseDouble(table[18]));
              prep.setDouble(20, Double.parseDouble(table[19]));
              prep.setString(21, (table[20]));
              prep.setString(22, (table[21]));
              prep.setString(23, (table[22]));
              prep.setString(24, (table[23]));
              prep.setString(25, (table[24]));
              prep.setString(26, (table[25]));
              prep.setString(27, (table[26]));
              prep.setString(28, (table[27]));
              prep.setDouble(29, Double.parseDouble(table[28]));
              prep.setDouble(30, Double.parseDouble(table[29]));
              prep.setString(31, (table[30]));
              prep.setString(32, (table[31]));
              prep.setString(33, (table[32]));
              prep.setString(34, (table[33]));
              prep.setString(35, (table[34]));
              prep.setString(36, (table[35]));
              prep.setString(37, (table[36]));
              prep.setString(38, (table[37]));
              prep.setString(39, (table[38]));
              prep.setString(40, (table[39]));
              prep.setString(41, (table[40]));
              prep.setDouble(42, Double.parseDouble(table[41]));
              prep.setDouble(43, Double.parseDouble(table[42]));
              prep.setDouble(44, Double.parseDouble(table[43]));
              prep.execute();
              conn.commit();
          }
          currentLine++;
      }
  }

  catch (Exception e)
  {
      e.printStackTrace();
  }

  finally
  {
      if(br == null)
      try
      {
          br.close();
      }

      catch (IOException e)
      {
          e.printStackTrace();
      }
  }

  System.out.println("müsste feddich sein");

}

1 个答案:

答案 0 :(得分:0)

您将currentLine初始化为0,第一次传入循环时条件if(currentLine > 0)为false,这就是未插入第一行的原因。

除非您删除了代码的某些重要部分,否则测试无效。 currenltLine仅从0开始递增,它永远不会为负,也不会为空。