读出.csv文件并将其保存为Java中的sql-lite

时间:2018-03-23 10:45:15

标签: javascript java sqlite

我必须在数据库中填入我的CSV中的数据(大约30列)。 我需要编写代码,用我的CSV中的数据自动填充我的数据库。我应该使用SQLite来做到这一点。(或者HSQL)

我对所有人都是新手,所以请不要粗鲁。

有可能吗? 有什么建议吗?

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////

编辑:所以我到目前为止。

package importCSV;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ImportData
{
  public ImportData()
  {

  }

  public static void main(String[] args) 
  {
      createDB("tollerName.db");
      read();
      createTable("tollerName.db");
  } 

  public static void createDB(String name)    
  {    
      String url = "jdbc:sqlite:C:/sqlite/db/" + name;       

      try(Connection conn = DriverManager.getConnection(url))    
      {
           if (conn != null) 
           {
                System.out.println("DB erstellt");
           }

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

public static void createTable(String name)      
{      
      String url = "jdbc:sqlite:C:/sqlite/db/" + name;        

      try
      {
          Connection conn = DriverManager.getConnection(url);

          String table = "table("
          + "Bauprojekt text primary key not null,"
          + "Bauprojektnummer integer not null,"
          + "Auftragsnummer integer not null,"
          + "Trassierung integer not null,"
          + "JahrSAP integer not null,"
          + "ProjektnummerSPA integer not null,"
          + "Anlagenzahl integer not null,"
          + "Aktuelle Leitungslaenge integer not null,"
          + "Vorgaenge zum Projekt integer not null,"
          + "Vertraege zum Projekt integer not null,"
          + "Firma text not null,"
          + "Flurst integer not null,"
          + "Blatt integer not null,"
          + "Verf text not null,"
          + "Anl text not null,"
          + "RE text not null,"
          + "LL integer not null,"
          + "Breite SS integer not null,"
          + "Flaeche RA integer not null,"
          + "Entsch integer not null,"
          + "Haupteigentuemer text not null,"
          + "Gemarkung text not null,"
          + "Grundbuchamt text not null,"
          + "Strase text not null,"
          + "Hausnummer integer not null,"
          + "Hausnummerzusatz text not null,"
          + "Ort text not null,"
          + "plz integer not null,"
          + "VkWert real not null,"
          + "prozent BPD real not null,"
          + "Anlage real not null,"
          + "GFR null not null,"
          + "GBA text not null,"
          + "Empfaenger text not null,"
          + "Bank text not null,"
          + "IBAN text not null,"
          + "BIC integer not null,"
          + "von text not null,"
          + "an text not null,"
          + "Jahr der Vergabe integer not null,"
          + "Zahlungsgrund text not null,"
          + "Netto real not null,"
          + "Aufwand real not null,"
          + "Umsatzsteuer real not null,"
          + ")";

          PreparedStatement pre;
          pre = conn.prepareStatement(table);
          pre.executeUpdate();
          pre.close();

      }
      catch (SQLException e) 
      {
           e.printStackTrace();       
      }
  System.out.println("done");
}

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

  try     
  {       
      br = new BufferedReader(new FileReader(csv));      
      while((line = br.readLine()) != null)       
      {       
          String[] table = line.split(csvSplitBy);      
      }
  } 

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

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

      catch (IOException e)
      {
          e.printStackTrace();
      }
  }
  System.out.println("Red");
  }
 }

这是我的所有代码。 我在这做错了什么? " println"命令执行但我仍然收到此错误:

org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (near "table": syntax error)
at org.sqlite.core.DB.newSQLException(DB.java:909)
at org.sqlite.core.DB.newSQLException(DB.java:921)
at org.sqlite.core.DB.throwex(DB.java:886)
at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
at org.sqlite.core.NativeDB.prepare(NativeDB.java:127)
at org.sqlite.core.DB.prepare(DB.java:227)
at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:41)
at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:48)
at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:263)
at     org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:235)
    at importCSV.ImportData.createTable(ImportData.java:99)
    at importCSV.ImportData.main(ImportData.java:23)

1 个答案:

答案 0 :(得分:0)

好吧,我使用类BufferedReader来读取csv文件。

一个例子是

    BufferedReader reader = null;

try {
    File file = new File("myFile.csv"); // path to "MyFile.csv"
    reader = new BufferedReader(new FileReader(file));

    String line;
    while ((line = reader.readLine()) != null) {
        String[] splitString = line.split(";"); // split by the separator
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

reader.readLine()会在.csv文件中为您提供准确的一行。之后你必须将你所拥有的String行拆分成任何行分隔的行(在大多数情况下,行由“;”分隔)。然后,您将每个元素都保存在一行中。

为了将您从.csv文件中读取的数据插入到sql-lite数据库中,请使用this example

修改

你的table-command有一些错误:

  • CREATE-command且缺少表名
  • 列名必须是一个单词(每个单词之间没有像“Jahr der Vergabe”那样的空格,这应该改为“jahr_der_vergabe”或“vergabejahr”。
  • 你在最后一个COLUMN-Annotation(Umsatzsteuer)中有一个逗号,它会给你一个错误。

这个表命令应该可以正常工作(我已经改变了一点,例如PLZ的类型,这不应该是一个数字,因为你不用它来计算):

String table = "CREATE TABLE Dienstbarkeit" +
                    "(" +
                    "  bauprojekt          NOT NULL TEXT PRIMARY KEY," +
                    "  bauprojektnummer    NOT NULL INTEGER         ," +
                    "  auftragsnummer      NOT NULL INTEGER         ," +
                    "  trassierung         NOT NULL INTEGER         ," +
                    "  jahrspa             NOT NULL INTEGER         ," +
                    "  projektnummerspa    NOT NULL INTEGER         ," +
                    "  anlagenzahl         NOT NULL INTEGER         ," +
                    "  akt_leitungslaenge  NOT NULL INTEGER         ," +
                    "  vorgaenge_zum_proj  NOT NULL INTEGER         ," +
                    "  vertraege_zum_proj  NOT NULL INTEGER         ," +
                    "  firma               NOT NULL TEXT            ," +
                    "  flurst              NOT NULL INTEGER         ," +
                    "  blatt               NOT NULL INTEGER         ," +
                    "  verf                NOT NULL TEXT            ," +
                    "  Verf                NOT NULL TEXT            ," +
                    "  Anl                 NOT NULL TEXT            ," +
                    "  RE                  NOT NULL TEXT            ," +
                    "  LL                  NOT NULL INTEGER         ," +
                    "  Breite_SS           NOT NULL INTEGER         ," +
                    "  Flaeche_RA          NOT NULL INTEGER         ," +
                    "  Entsch              NOT NULL INTEGER         ," +
                    "  Haupteigentuemer    NOT NULL TEXT            ," +
                    "  Gemarkung           NOT NULL TEXT            ," +
                    "  Grundbuchamt        NOT NULL TEXT            ," +
                    "  Strasse             NOT NULL TEXT            ," +
                    "  Hausnummer          NOT NULL INTEGER         ," +
                    "  Hausnummerzusatz    NOT NULL TEXT            ," +
                    "  Ort                 NOT NULL TEXT            ," +
                    "  plz                 NOT NULL TEXT            ," +
                    "  VkWert              NOT NULL REAL            ," +
                    "  prozent_BPD         NOT NULL REAL            ," +
                    "  Anlage              NOT NULL REAL            ," +
                    "  GFR                 NOT NULL TEXT            ," +
                    "  GBA                 NOT NULL TEXT            ," +
                    "  Empfaenger          NOT NULL TEXT            ," +
                    "  Bank                NOT NULL TEXT            ," +
                    "  IBAN                NOT NULL TEXT            ," +
                    "  BIC                 NOT NULL INTEGER         ," +
                    "  von                 NOT NULL TEXT            ," +
                    "  an                  NOT NULL TEXT            ," +
                    "  jahr_der_vergabe    NOT NULL INTEGER         ," +
                    "  zahlungsgrund       NOT NULL TEXT            ," +
                    "  Netto               NOT NULL REAL            ," +
                    "  Aufwand             NOT NULL REAL            ," +
                    "  Umsatzsteuer        NOT NULL REAL             " +
                    ")";