• SQLite 安装和 Java 使用教程


    SQLite是一个C语言库,它实现了一个小型、快速、自包含、高可靠性、功能齐全的SQL数据库引擎。SQLite是世界上使用最多的数据库引擎。SQLite内置于所有手机和大多数计算机中,并捆绑在人们每天使用的无数其他应用程序中。

    SQLite文件格式稳定、跨平台、向后兼容,开发人员承诺在2050年保持这种格式。SQLite数据库文件通常用作在系统之间传输丰富内容的容器,以及数据的长期存档格式。目前有超过1万亿个SQLite数据库在积极使用

    SQLite 官网:https://www.sqlite.org/index.html

    目录

    1、安装 SQLite

    2、新建数据库

    3、新建表

    4、Java 操作 SQLite

    4.1、普通类型存储

    4.2、二进制类型存储


    1、安装 SQLite

    官网下载 SQLite

    下载地址:https://www.sqlite.org/download.html

     根据自己的电脑系统选择

    下载后将其解压

    解压

    2、新建数据库

    双击打开 sqlite3.exe

    弹出命令行窗体

    新建数据库命令

    .open test.db

    新建名称是 test 的数据库

    新建完成后,会在当前目录下生成 test.db 的文件,数据库创建完成

    3、新建表

    新建表可以使用命令

    1. CREATE TABLE user (
    2. id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    3. name TEXT
    4. );

     新建 user 表,有 id 和 name 字段,id是主键自增,name是 TEXT 字符串文本类型

    SQLite 的类型有

    类型说明
    NULL空值
    INTEGER带符号的整型
    REAL浮点数字
    TEXT字符串
    BLOB二进制对象

     除此之外可以使用 Navicat 连接建表

    选择 SQLite

    连接名随意

    选择数据库文件,点击确定即可

    新建表

    新建表,有3个字段, id, name,price

    新建完成

    4、Java 操作 SQLite

    新建 maven 项目 sqlite-learn

    引入 sqlite-jdbc 依赖

    1. <dependency>
    2. <groupId>org.xerialgroupId>
    3. <artifactId>sqlite-jdbcartifactId>
    4. <version>3.41.2.1version>
    5. dependency>

    sqlite-learn 项目 pom 文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>com.wsjzzcbqgroupId>
    7. <artifactId>sqlite-learnartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.xerialgroupId>
    12. <artifactId>sqlite-jdbcartifactId>
    13. <version>3.41.2.1version>
    14. dependency>
    15. dependencies>
    16. <build>
    17. <plugins>
    18. <plugin>
    19. <artifactId>maven-compiler-pluginartifactId>
    20. <configuration>
    21. <source>11source>
    22. <target>11target>
    23. configuration>
    24. plugin>
    25. plugins>
    26. build>
    27. project>

    4.1、普通类型存储

    增删改查代码

    1. package com.wsjzzcbq;
    2. import java.sql.*;
    3. /**
    4. * SqliteDemo
    5. *
    6. * @author wsjz
    7. * @date 2023/11/17
    8. */
    9. public class SqliteDemo {
    10. public static void main(String[] args) throws ClassNotFoundException, SQLException {
    11. Class.forName("org.sqlite.JDBC");
    12. //SQLite 数据库文件
    13. String dbFile = "D:\\tmp\\sql\\sqlite-tools-win-x64-3440000\\test.db";
    14. String url = "jdbc:sqlite:" + dbFile;
    15. Connection conn = DriverManager.getConnection(url);
    16. //添加
    17. insert(conn);
    18. //查询
    19. select(conn);
    20. //修改
    21. update(conn);
    22. //删除
    23. delete(conn);
    24. conn.close();
    25. }
    26. private static void select(Connection connection) throws SQLException {
    27. String sql = "select * from user";
    28. Statement statement = connection.createStatement();
    29. ResultSet rs = statement.executeQuery(sql);
    30. while (rs.next()) {
    31. int id = rs.getInt("id");
    32. String name = rs.getString("name");
    33. System.out.println(id);
    34. System.out.println(name);
    35. }
    36. rs.close();
    37. statement.close();
    38. }
    39. private static void insert(Connection connection) throws SQLException {
    40. String sql = "insert into user( name) values('小丽')";
    41. Statement stat = connection.createStatement();
    42. stat.executeUpdate(sql);
    43. stat.close();
    44. }
    45. private static void update(Connection connection) throws SQLException {
    46. String sql = "update user set name = ? where id = ?";
    47. PreparedStatement ps = connection.prepareStatement(sql);
    48. ps.setObject(1, "小雪花");
    49. ps.setObject(2, 1);
    50. ps.executeUpdate();
    51. ps.close();
    52. }
    53. private static void delete(Connection connection) throws SQLException {
    54. String sql = "delete from user where id = ?";
    55. PreparedStatement ps = connection.prepareStatement(sql);
    56. ps.setObject(1, 1);
    57. ps.executeUpdate();
    58. ps.close();
    59. }
    60. }

    运行测试

    测试添加

    测试修改

    测试删除

    shop 表代码

    1. package com.wsjzzcbq;
    2. import java.sql.*;
    3. /**
    4. * SqliteDemo2
    5. *
    6. * @author wsjz
    7. * @date 2023/11/17
    8. */
    9. public class SqliteDemo2 {
    10. public static void main(String[] args) throws ClassNotFoundException, SQLException {
    11. Class.forName("org.sqlite.JDBC");
    12. //SQLite 数据库文件
    13. String dbFile = "D:\\tmp\\sql\\sqlite-tools-win-x64-3440000\\test.db";
    14. String url = "jdbc:sqlite:" + dbFile;
    15. Connection conn = DriverManager.getConnection(url);
    16. //添加
    17. insert(conn);
    18. //查询
    19. select(conn);
    20. conn.close();
    21. }
    22. private static void insert(Connection connection) throws SQLException {
    23. String sql = "insert into shop(name, price) values('水浒传', 20)";
    24. Statement stat = connection.createStatement();
    25. stat.executeUpdate(sql);
    26. stat.close();
    27. }
    28. private static void select(Connection connection) throws SQLException {
    29. String sql = "select * from shop";
    30. Statement statement = connection.createStatement();
    31. ResultSet rs = statement.executeQuery(sql);
    32. while (rs.next()) {
    33. int id = rs.getInt("id");
    34. String name = rs.getString("name");
    35. double price = rs.getDouble("price");
    36. System.out.println(id);
    37. System.out.println(name);
    38. System.out.println(price);
    39. }
    40. rs.close();
    41. statement.close();
    42. }
    43. }

    运行测试

    4.2、二进制类型存储

    储存二进制图片文件

    1. package com.wsjzzcbq;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.nio.file.Files;
    7. import java.nio.file.Paths;
    8. import java.sql.*;
    9. /**
    10. * SqliteDemo3
    11. *
    12. * @author wsjz
    13. * @date 2023/11/17
    14. */
    15. public class SqliteDemo3 {
    16. public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
    17. Class.forName("org.sqlite.JDBC");
    18. //SQLite 数据库文件
    19. String dbFile = "D:\\tmp\\sql\\sqlite-tools-win-x64-3440000\\test.db";
    20. String url = "jdbc:sqlite:" + dbFile;
    21. Connection conn = DriverManager.getConnection(url);
    22. //新建表
    23. createTable(conn);
    24. //添加
    25. insert(conn);
    26. //查询
    27. select(conn);
    28. conn.close();
    29. }
    30. private static void createTable(Connection connection) throws SQLException {
    31. String sql = "CREATE TABLE IF NOT EXISTS img (name TEXT, image BLOB)";
    32. Statement statement = connection.createStatement();
    33. statement.executeUpdate(sql);
    34. statement.close();
    35. }
    36. private static void insert(Connection connection) throws SQLException, IOException {
    37. String sql = "insert into img(name, image) values(?, ?)";
    38. PreparedStatement ps = connection.prepareStatement(sql);
    39. ps.setObject(1, "怀素自叙帖");
    40. String filePath = "D:\\tmp\\img\\huaisu.png";
    41. byte[] bytes = Files.readAllBytes(Paths.get(filePath));
    42. InputStream inputStream = Files.newInputStream(Paths.get(filePath));
    43. //添加图片文件
    44. ps.setBinaryStream(2, inputStream, bytes.length);
    45. ps.executeUpdate();
    46. ps.close();
    47. }
    48. private static void select(Connection connection) throws SQLException, IOException {
    49. String sql = "select * from img";
    50. Statement statement = connection.createStatement();
    51. ResultSet rs = statement.executeQuery(sql);
    52. while (rs.next()) {
    53. //获取name
    54. String name = rs.getString("name");
    55. System.out.println(name);
    56. //获取图片文件
    57. InputStream inputStream = rs.getBinaryStream("image");
    58. String filePath = "D:\\tmp\\img\\huaisu2.png";
    59. Files.copy(inputStream, Paths.get(filePath));
    60. }
    61. rs.close();
    62. statement.close();
    63. }
    64. }

    测试运行

    至此完

  • 相关阅读:
    sklearn中的TfidfTransformer和gensim中的TfidfModel的区别
    React生命周期和响应式原理(Fiber架构)
    在 C# CLR 中学习 C++ 之了解 extern
    蓝桥等考Python组别四级001
    vuex报错 Cannot destructure property ‘commit‘ of ‘undefined
    JAR防止反编译
    微服务框架 SpringCloud微服务架构 22 DSL 查询语法 22.4 地理查询
    超短高手赚大钱的必备3个问题
    CEP开发基础知识-AI|PS|AE插件-事件机制-文件操作-界面颜色
    数据赋能(121)——体系:数据清洗——实施过程、应用特点
  • 原文地址:https://blog.csdn.net/wsjzzcbq/article/details/134468069