SQLite

SQLite 是一个无服务器的、零配置的、事务性的的开源数据库引擎。

SQLite 简介

SQLite 是一个C语言编写的轻量级、全功能、无服务器、零配置的的开源数据库引擎。

SQLite 的设计目标是嵌入式的数据库,很多嵌入式产品中都使用了它。SQLite 十分轻量,占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。SQLite 能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。

SQLite 大小只有 3M 左右,可以将整个 SQLite 嵌入到应用中,而不用采用传统的客户端/服务器(Client/Server)的架构。这样做的好处就是非常轻便,在许多智能设备和应用中都可以使用 SQLite,比如微信就采用了 SQLite 作为本地聊天记录的存储。

SQLite 语法

和 mysql 差不多,不做详细记录,后面有遇到特殊的在做记录。

SQLite JAVA Client

(1)在官方下载地址下载 sqlite-jdbc-(VERSION).jar ,然后将 jar 包放在项目中的 classpath。

(2)通过 API 打开一个 SQLite 数据库连接。

执行方法:

1
2
3
4
5
6
7
8
> javac Sample.java
> java -classpath ".;sqlite-jdbc-(VERSION).jar" Sample # in Windows
or
> java -classpath ".:sqlite-jdbc-(VERSION).jar" Sample # in Mac or Linux
name = leo
id = 1
name = yui
id = 2

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Sample {
public static void main(String[] args) {
Connection connection = null;
try {
// 创建数据库连接
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // 设置 sql 执行超时时间为 30s

statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while (rs.next()) {
// 读取结果集
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}
} catch (SQLException e) {
// 如果错误信息是 "out of memory",可能是找不到数据库文件
System.err.println(e.getMessage());
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// 关闭连接失败
System.err.println(e.getMessage());
}
}
}
}

如何指定数据库文件

Windows

1
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");

Unix (Linux, Mac OS X, etc)

1
Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");

如何使用内存数据库

1
Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:");

Sqlite 整合入 springboot

配置依赖:

1
2
3
4
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>

配置文件:application.properties

1
2
3
4
spring.datasource.sqlite.url = jdbc:sqlite:testDB.db
spring.datasource.sqlite.driver-class-name = org.sqlite.JDBC
spring.datasource.sqlite.username =
spring.datasource.sqlite.password =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

@Slf4j
@SpringBootApplication
@EnableScheduling
public class StartDataApplication implements CommandLineRunner {

private final JdbcTemplate sqliteJdbcTemplate;

private final Environment environment;

@Resource
private SftpConfiguration sftpConfiguration;

public StartDataApplication(JdbcTemplate sqliteJdbcTemplate, Environment environment) {
this.sqliteJdbcTemplate = sqliteJdbcTemplate;
this.environment = environment;
}

public static void main(String[] args) {
SpringApplication.run(StartDataApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
log.info("server.port={}", environment.getProperty("server.port"));
log.info(sftpConfiguration.toString());
SQLiteDataSource sqliteDataSource = (SQLiteDataSource) sqliteJdbcTemplate.getDataSource();
Connection sqliteConn;
if (sqliteDataSource != null){
sqliteDataSource.setUrl("jdbc:sqlite:E:\\workspace_mine\\backup\\XZFD039_01_02_XZFD039_01_02024_2025-05-27-21-26-22-539.cms");
sqliteConn = sqliteDataSource.getConnection();
}else {
log.error("sqlite连接数据源失败!");
return;
}

if (sqliteConn != null) {
log.info("sqlite连接数据源成功!数据源 Url: {}", sqliteConn.getMetaData().getURL());
} else {
log.error("sqlite连接数据源失败!");
return;
}
}
}

参考资料