Skip to content

Latest commit

 

History

History
165 lines (116 loc) · 4.34 KB

Lealone数据库快速入门.md

File metadata and controls

165 lines (116 loc) · 4.34 KB

1. Lealone 数据库快速入门

1.1. 运行需要

1.2. 下载 Lealone

lealone-6.0.1.jar

Lealone 只有一个 jar 包,下载下来之后随意放到一个目录即可

也可以从源代码构建最新版本,请阅读文档: 从源码构建 Lealone

1.3. 启动 Lealone 数据库

打开一个新的命令行窗口,运行: java -jar lealone-6.0.1.jar

Lealone version: 6.0.1
Use default config
Base dir: ./lealone_data
Init storage engines: 5 ms
Init transaction engines: 46 ms
Init sql engines: 4 ms
Init protocol server engines: 13 ms
Init lealone database: 119 ms
TcpServer started, host: 127.0.0.1, port: 9210
Total time: 207 ms (Load config: 2 ms, Init: 201 ms, Start: 4 ms)
Exit with Ctrl+C

要停止 Lealone,直接按 Ctrl + C

1.4. 使用 Lealone 客户端执行 SQL 语句

打开一个新的命令行窗口,运行: java -jar lealone-6.0.1.jar -client

默认用 root 用户连到 127.0.0.1:9210,密码为空

默认数据库是 lealone,可以通过 -database 参数指定其他数据库

Welcome to Lealone Shell 6.0.1
Connect to jdbc:lealone:tcp://localhost:9210/lealone
Commands are case insensitive; SQL statements end with ';'
help or ?          Display this help
list               Toggle result list / stack trace mode
maxwidth or md     Set maximum column width (default is 100)
autocommit or ac   Enable or disable autocommit
history or h       Show the last 20 statements
reconnect or rc    Reconnect the database
quit or exit       Close the connection and exit

sql> CREATE TABLE IF NOT EXISTS test (f1 int primary key, f2 long);
(Update count: 0, 7 ms)

sql> INSERT INTO test(f1, f2) VALUES(1, 2);
(Update count: 1, 2 ms)

sql> SELECT * FROM test;
+----+----+
| F1 | F2 |
+----+----+
| 1  | 2  |
+----+----+
(1 row, 8 ms)

sql> UPDATE test SET f2 = 20;
(Update count: 1, 1 ms)

sql> SELECT * FROM test;
+----+----+
| F1 | F2 |
+----+----+
| 1  | 20 |
+----+----+
(1 row, 2 ms)

sql> DELETE FROM test;
(Update count: 1, 2 ms)

sql> SELECT * FROM test;
+----+----+
| F1 | F2 |
+----+----+
+----+----+
(0 rows, 1 ms)

1.5. CRUD Example

只需要懂 JDBC 和 SQL 就可以轻松看懂下面的代码:

import java.sql.*;

public class CRUDExample {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:lealone:tcp://localhost:9210/lealone";
        Connection conn = DriverManager.getConnection(url, "root", "");
        Statement stmt = conn.createStatement();

        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS test (f1 int primary key, f2 long)");
        stmt.executeUpdate("INSERT INTO test(f1, f2) VALUES(1, 1)");
        stmt.executeUpdate("UPDATE test SET f2 = 2 WHERE f1 = 1");
        ResultSet rs = stmt.executeQuery("SELECT * FROM test");
        while (rs.next()) {
            System.out.println("f1=" + rs.getInt(1) + " f2=" + rs.getLong(2));
            System.out.println();
        }
        rs.close();
        stmt.executeUpdate("DELETE FROM test WHERE f1 = 1");
        stmt.executeUpdate("DROP TABLE IF EXISTS test");
        stmt.close();
        conn.close();
    }
}

把上面的代码存到一个 CRUDExample.java 文件 (目录随意)

编译:

javac CRUDExample.java

运行:

java -cp .;lealone-6.0.1.jar CRUDExample

1.6. Lealone Client Maven 依赖

<dependencies>
    <dependency>
        <groupId>com.lealone</groupId>
        <artifactId>lealone-client</artifactId>
        <version>6.0.1</version>
    </dependency>
</dependencies>

2. UI 客户端

可以使用 DBeaver 这类 JDBC UI 客户端访问 Lealone

DBeaver 配置文档

3. SQL Reference

因为 Lealone 的 SQL 引擎从 H2 数据库 的 SQL 引擎发展而来, 所以 Lealone 的 SQL 用法与 H2 数据库一样。