Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4182

Deleting temporary database files when using CSVREAD in H2

I am using the CSVREADER in h2 to process a CSV file, which works really great.

    final String SQL = "SELECT name, age, address FROM CSVREAD('/home/testuser/csvfile.csv', null, 'fieldSeparator=,')";
    DriverManager.registerDriver(new org.h2.Driver());
    try (Connection c = DriverManager.getConnection("jdbc:h2:" + filename, "sa", "");
            PreparedStatement statement = c.prepareStatement(SQL);
            ResultSet r = statement.executeQuery()) {
        while (r.next()) {
            // processing data...
        }
        LOGGER.debug("Processed data");
    }

When this code run, it creates temporary files (csvfile.csv.mv.db and csvfile.csv.trace.mv.db). Is there an elegant file to get H2 to clean up after itself, or do I manually have to do it on its behalf?

Is there a property to set, maybe?

Upvotes: 0

Views: 224

Answers (1)

Thomas Mueller
Thomas Mueller

Reputation: 50087

You can use the CSV tool directly, without opening a database:

Csv csv = new Csv();
csv.setOptions("fieldSeparator=,");
String fileName = "/home/testuser/csvfile.csv";
ResultSet rs = csv.read(fileName, null, null);
while (rs.next()) {
    // processing data...
}
rs.close();

Upvotes: 1

Related Questions