使用 H2,您可以在数据库 URL 本身中初始化数据库。示例:您有一个 SQL 脚本“start.sql”,其中包含要初始化的所有脚本。这还可以包括从 CSV 文件创建表。然后使用表单的数据库 URL jdbc:h2:~/temp/test;init=runscript from '~/temp/start.sql'
。start.sql 可能看起来像这样(这是我正在处理的一个示例 - 它显示了如何从 CSV 文件创建表):
create table if not exists location(id int primary key, country varchar,
region varchar, city varchar, postalCode varchar, latitude float, longitude float,
metroCode varchar, areaCode varchar)
as select * from csvread('~/Downloads/GeoLiteCity/GeoLiteCity-Location.csv');
create table if not exists blocks(start long, end long primary key, location int)
as select * from csvread('~/Downloads/GeoLiteCity/GeoLiteCity-Blocks.csv');
create alias if not exists ip2id deterministic as $$
long ip2id(String s) {
String[] x = s.split("\\.");
return (Long.parseLong(x[0]) << 24) + (Long.parseLong(x[1]) << 16) +
(Long.parseLong(x[2]) << 8) + Long.parseLong(x[3]);
} $$;
create alias if not exists id2ip deterministic as $$
String id2ip(long x) {
return (x >> 24) + "." + ((x >> 16) & 255) + "." +
((x >> 8) & 255) + "." + (x & 255);
} $$;