Skip to content

Instantly share code, notes, and snippets.

@hexiyou
Forked from johnnian/H2Command
Created December 20, 2022 05:16
Show Gist options
  • Save hexiyou/aaf2028f1fc88f6e3202dd2286110860 to your computer and use it in GitHub Desktop.
Save hexiyou/aaf2028f1fc88f6e3202dd2286110860 to your computer and use it in GitHub Desktop.
H2命令行
## 命令行工具
H2数据库提供了一组命令行工具,如果你需要了解这些工具,使用参数`-?`,如:
java -cp h2*.jar org.h2.tools.Backup -?
命令行工具有:
* Backup 创建数据库备份
* ChangeFileEncryption 允许改变文件加密密码和数据库的加密算法
* Console 启动基于浏览器的 H2 控制台
* ConvertTraceFile 转换 `.trace.db` 文件到 Java 应用和 SQL 脚本
* CreateCluster 从一个独立的数据库服务创建集群
* DeleteDbFiles 删除所有的数据库文件
* Recover 恢复损坏的数据库
* Restore 从数据库备份中恢复数据库
* RunScript 运行数据库 SQL 脚本
* Script 为数据库备份或迁移导出 SQL 脚本
* Server 启动 H2 服务模式
* Shell 命令行工具
这些工具也能在程序中通过调用相应的方法来使用,相关详细的调用说明,请参考 JavaDoc 文档。
## Shell 工具
Shell 工具是最简单的命令行工具,开始时,输入:
java -cp h2*.jar org.h2.tools.Shell
你会要求输入数据库URL、JDBC 驱动、用户名和密码。连接设置也可以作为命令行参数设置。连接后,你会得到的列表选项。内置命令不需要以分号结束,但只是执行 SQL 语句,需要以分号 `;`作为结束。这允许输入多行语句:
sql> select * from test
...> where id = 0;
默认情况下,结果是打印成表。结果有许多列,考虑使用模式列表:
sql> list
Result list mode is now on
sql> select * from test;
ID : 1
NAME: Hello
ID : 2
NAME: World
(2 rows, 0 ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment