SQLite实例

SQLite学习笔记

使用SQLite-java分三步:
1.连接数据库
2.创建事务状态
3.执行数据库事务

连接数据库

要连接数据库,需要用到java.sql.Connection类,先创建一个Connection实例,再通过DriverManager.getConnection({String})注入路径,取得连接。路径正确的情况下,有该数据库则打开,没该数据库则自动创建,但路径错误会直接报错

1
2
Connection connection = null;
connection = DriverManager.getConnection("jdbc:sqlite:src/main/resources/sample.db");

创建事务状态

在Connection实例上直接用createStatement(),创建事务状态,以进行后续的数据库操作,而setQueryTimeout({int})可以限定事务执行的最长时间。

1
2
Statement statement = connection.createStatement();
statement.setQueryTimeout(30);

执行数据库事务

连接到数据库,并创建好事务状态后,用executeUpdate()方法直接执行数据库语句

创建表

SQLite语句:create table {表名} ({列名1 类型1,列名2 类型2,…})

1
statement.executeUpdate("create table person(id integer not null, name string not null)");

插入数据

SQLite语句:insert into {表名} values({结构})

1
statement.executeUpdate("insert into person values(1, 'wz')");

查询数据

SQLite语句:select {列名} from {表名}

1
statement.executeUpdate("select * from person");

更新数据

SQLite语句:update {表名} set {列名} = {值} where {列名} = {值}

1
statement.executeUpdate("update person set name = 'zz' where id = 1")

删除数据

SQLite语句:delete from {表名} where {列名} = {值}

1
statement.executeUpdate("delete from person where name = 'zz'");

删除表

SQLite语句:drop table if exists {表名}

1
statement.executeUpdate("drop table if exists person");

代码实例

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.sql.*;

public class jdbcSample {
public static void main(String[] args) {

// 声明连接connection
Connection connection = null;
try {
// 连接到数据库
connection = DriverManager.getConnection("jdbc:sqlite:src/main/resources/sample.db");
// 事务
Statement statement = connection.createStatement();
// 设定SQL执行超时时间(秒),超时则抛出SQLException
statement.setQueryTimeout(30);

// 删除原有的person表
statement.executeUpdate("drop table if exists person");
// 创建person表以及声明其结构
statement.executeUpdate("create table person(id integer not null, name string not null)");
// 插入数据
statement.executeUpdate("insert into person values(1, 'wz')");
statement.executeUpdate("insert into person values(2, 'yz')");
// 选择数据,*为全部,放入ResultSet实例中
ResultSet rs = statement.executeQuery("select * from person");
// 遍历输出
while(rs.next()) {
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}

System.out.println("---------------------------------");

// 更新数据
statement.executeUpdate("update person set name = 'zz' where id = 1");
statement.executeUpdate("update person set id = 3 where name = 'yz'");
rs = statement.executeQuery("select * from person");
while(rs.next()) {
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}

System.out.println("---------------------------------");
// 删除数据
statement.executeUpdate("delete from person where name = 'zz'");
rs = statement.executeQuery("select * from person");
while(rs.next()) {
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}

} catch(SQLException e) {
// if the error massage is "out of memory" ,
// it probably means no database file is found.
System.err.println(e.getMessage());
} finally {
try {
if(connection != null)
connection.close();
} catch(SQLException e) {
// connection close failed.
System.err.println(e);
}
}
}
}

其他

alter 修改

在表中添加列:

1
2
ALTER TABLE table_name
ADD column_name datatype

删除表中的列:

1
2
ALTER TABLE table_name 
DROP COLUMN column_name