Javascript 读取.csv文件并将其保存到Java中的SQLLite中
我必须用CSV中的数据(大约30列)填充数据库。 我需要编写代码,用CSV中的数据自动填充数据库。我应该使用SQLite来实现这一点 我对这一切都有点陌生,所以请不要无礼 有可能吗? 有什么建议吗 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 编辑:所以我走了这么远Javascript 读取.csv文件并将其保存到Java中的SQLLite中,javascript,java,sqlite,Javascript,Java,Sqlite,我必须用CSV中的数据(大约30列)填充数据库。 我需要编写代码,用CSV中的数据自动填充数据库。我应该使用SQLite来实现这一点 我对这一切都有点陌生,所以请不要无礼 有可能吗? 有什么建议吗 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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)
org.sqlite.SQLiteException:[sqlite\u ERROR]SQL错误或缺少数据库(靠近“表”:语法错误)
位于org.sqlite.core.DB.newSQLException(DB.java:909)
位于org.sqlite.core.DB.newSQLException(DB.java:921)
位于org.sqlite.core.DB.throwex(DB.java:886)
在org.sqlite.core.NativeDB.prepare_utf8(本机方法)
位于org.sqlite.core.NativeDB.prepare(NativeDB.java:127)
位于org.sqlite.core.DB.prepare(DB.java:227)
位于org.sqlite.core.CorePreparedStatement.(CorePreparedStatement.java:41)
位于org.sqlite.jdbc3.JDBC3PreparedStatement。(JDBC3PreparedStatement.java:30)
位于org.sqlite.jdbc4.JDBC4PreparedStatement。(JDBC4PreparedStatement.java:19)
位于org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:48)
位于org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:263)
位于org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:235)
在importsv.ImportData.createTable(ImportData.java:99)中
位于importsv.ImportData.main(ImportData.java:23)
嗯,我使用类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文件中提供精确的一行。之后,您必须将您得到的字符串行拆分为任何以“;”分隔的行(在大多数情况下,该行以“;”分隔)。然后,您可以使用保存在一行中的每个元素
要将从.csv文件读取的数据插入sql lite数据库,请使用
编辑:
您的table命令有一些错误:
- 缺少CREATE命令和表名
- 列名必须是一个单词(每个单词之间没有空格,就像“Jahr der Vergabe”中一样),应该改为“Jahr_der_Vergabe”或“vergabejahr”
- 在最后一列注释(Umsatzsteuer)中有一个逗号,这将给您一个错误
是的,这是可能的。到目前为止你们试过什么?tbh。我以前从来并没有做过这样的事情。我对从文件中读取数据是新手。所以一些线索,例如我应该使用什么方法或类别会非常有用。我必须做什么,所以我的数据库中充满了来自csv的数据?我更新了我的答案是的,我也想到了这个!比你能解释一下我这样做到底得到了什么吗?
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 " +
")";