【问题标题】:import data from an external file(csv) into embedded sqlite database [duplicate]将外部文件(csv)中的数据导入嵌入式sqlite数据库[重复]
【发布时间】: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();
         }
         
     }
 }
 

}

【问题讨论】:

    标签: java eclipse sqlite


    【解决方案1】:

    在企业应用程序中,他们使用像 liquibase 这样的数据库迁移工具来做到这一点。 它使用自动配置与 Spring Boot 很好地集成。但是如果你不使用 spring 你可以自己配置它。 您将拥有一个这样的 XML 文件。

    <?xml version="1.0" encoding="utf-8"?>
    <databaseChangeLog
             xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
             xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog 
             http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd
                        http://www.liquibase.org/xml/ns/dbchangelog-ext 
              http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    
    <changeSet id="11111" author="me">
        <loadData
                  file="data/users.csv"
                  separator=";"
                  tableName="user"/>
    
    </changeSet>
    </databaseChangeLog>
    

    您可以像这样将数据写入 CSV 文件

    id;login;first_name;last_name;email
    1;John;John;John;John@localhost
    

    【讨论】:

    • 抱歉我不太明白这个方法。有没有办法可以将我的代码编辑为从 csv 文件中读取?
    • 如果您想读取 CSV 文件,可以查看此 baeldung.com/java-csv-file-array,但 liquibase 是解决此问题的更通用方法,您可以查看有关如何将其集成到代码中的答案 @987654322 @
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2012-11-08
    • 2012-05-04
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多