在使用postgres将txt文件中的数据插入sql表时遇到麻烦



我正在创建一个程序,该程序具有将数据从文本文件插入到sql表中的方法,(在eclipse上使用postgres与java)。

然而,我不断得到一个返回消息说0行插入,还有一个语法错误,我似乎找不到我的生命。

编辑:有人指出,我的composedLine是不完整的,我意识到这就是为什么它不工作,虽然我不确定如何将数据从文本文件连接到composedLine的语句,所以我可以插入该数据。

这是我首先创建表的方法

public static void createTable(Connection connection,
String tableDescription) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("CREATE TABLE " + tableDescription);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

下面是我插入表的方法:

public static int insertIntoTableFromFile(Connection connection,
String table, String filename) {



int numRows = 0;
String currentLine = null;
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
Statement st = connection.createStatement();
while ((currentLine = br.readLine())!= null) {
String[] values = currentLine.split(".");
String composedLine = "INSERT INTO " + table + " VALUES (";
numRows = st.executeUpdate(composedLine);

}

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

}

将感谢任何指示!谢谢你

通常类文本数据库依赖于一些delimiter,例如点.,逗号,,加号+,美元$等。在数据库表中插入行的常用过程使用命令INSERT来处理文本文件:它根据分隔符解析行,然后插入解析后的数据,用逗号分隔,如下所示:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

您必须确保从内到外的每个代码步骤都是正确的:结果字符串、连接、解析的数据。在证明每一种食材都值得烹饪之后,你可以用正确的方法把它们组合在一起。

来源:https://www.w3schools.com/sql/sql_insert.asp