【发布时间】:2020-11-17 20:05:54
【问题描述】:
我是 SQLite 的新手,我想将数据从 csv 等外部文件导入到这个嵌入式数据库中,而不是手动添加数据。例如,在这种情况下,我手动添加了 John McNeil 和 Paul Smith。有没有一种方法可以不通过读取包含名和姓的列的 csv 文件来手动添加。
public class SQLiteTest {
private static Connection con;
private static boolean hasData = false;
private void getConnection() throws ClassNotFoundException, SQLException {
// sqlite driver
Class.forName("org.sqlite.JDBC");
// database path, if it's new database, it will be created in the project folder
con = DriverManager.getConnection("jdbc:sqlite:SQLiteTest1.db");
initialise();
}
public void addUser(String firstname, String lastname) throws ClassNotFoundException, SQLException {
if(con == null) {
// get connection
getConnection();
}
PreparedStatement prep = con
.prepareStatement("insert into user values(?,?,?);");
prep.setString(2, firstname);
prep.setString(3, lastname);
prep.execute();
}
public ResultSet displayUsers() throws SQLException, ClassNotFoundException {
if(con == null) {
// get connection
getConnection();
}
Statement state = con.createStatement();
ResultSet res = state.executeQuery("select fname, lname from user");
return res;
}
private void initialise() throws SQLException {
if( !hasData ) {
hasData = true;
// check for database table
Statement state = con.createStatement();
ResultSet res = state.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='user'");
if( !res.next()) {
System.out.println("Building the User table with prepopulated values.");
// need to build the table
Statement state2 = con.createStatement();
state2.executeUpdate("create table user(id integer,"
+ "fName varchar(60)," + "lname varchar(60)," + "primary key (id));");
// inserting some sample data
PreparedStatement prep = con.prepareStatement("insert into user values(?,?,?);");
prep.setString(2, "John");
prep.setString(3, "McNeil");
prep.execute();
PreparedStatement prep2 = con.prepareStatement("insert into user values(?,?,?);");
prep2.setString(2, "Paul");
prep2.setString(3, "Smith");
prep2.execute();
}
}
}
}
【问题讨论】: