sqlite3 jdbc 只读模式

sqlite3 jdbc 只读 无效方法:

  1. “jdbc:sqlite:/bal/work_home/fn.db?readonly=true”
    导致 fn.db?readonly=true 会被当成是数据库文件名

  2. “jdbc:sqlite:/bal/work_home/fn.db?jdbc.explicit_readonly=true”
    参考了pragmaReadOnly

sqlite3 jdbc 只读 有效方法 举例

package com.zzz;

import com.zzz.sqlite3db.fn.entity.Func;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

import java.util.List;
import java.util.Properties;

import static com.zzz.utils.JavaVersionUtil.assertJavaVer1_8;

public class SpringJdbcSqlite3Demo {
    public static void main(String[] args){
        assertJavaVer1_8();

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(org.sqlite.JDBC.class.getCanonicalName());
        dataSource.setUrl("jdbc:sqlite:/bal/work_home/fn.db");
        dataSource.setUsername("");
        dataSource.setPassword("");

		// sqlite3 jdbc  只读模式打开了sqlite数据库
        Properties properties=new Properties();
        properties.setProperty("open_mode","1");
        dataSource.setConnectionProperties(properties);

        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);


        //正常执行
        Integer fnAbsLctId0=jdbcTemplate.queryForObject("select fnAbsLctId from tbl_Func limit 1", Integer.class);

        //正常查询到10条记录
        List<Func> funcList = jdbcTemplate.query("select * from tbl_Func limit 10", new BeanPropertyRowMapper<>(Func.class));

        //
        /**sqlite3 jdbc  只读模式打开了sqlite数据库
         当执行   delete 语句时 报错如下:
         Caused by: org.sqlite.SQLiteException: [SQLITE_READONLY] Attempt to write a readonly database (attempt to write a readonly database)



         */
        int cnt = jdbcTemplate.update("delete from tbl_Func where fnAbsLctId = 300");

        boolean end=true;
    }
}

寻找过程如下:

  1. sqlite-jdbc-3.42.0.0-sources.jar!/org/sqlite/SQLiteConnection.java
private static DB open(String url, String origFileName, Properties props) throws SQLException {...} 
//注意字典props中有 'open_mode=1
  1. sqlite-jdbc-3.42.0.0-sources.jar!/org/sqlite/SQLiteConfig.java
public enum Pragma {
OPEN_MODE("open_mode", "Database open-mode flag", null), 	
//实践表明 open_mode设置为1 即 表示 只读模式打开数据库 相当于 命令 'sqlite3 -readonly fn.db'
...
}
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值