8

I have a large database that I want to open in read-only mode... I'm using SQLiteJDBC and am not sure what to use to get it to work as read-only.

Can anyone help?

Jason S
  • 184,598
  • 164
  • 608
  • 970

5 Answers5

13

Test demonstrate how to set connection to be read-only:

SQLiteConfig config = new SQLiteConfig();

config.setReadOnly(true); 

Connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db",
config.toProperties());
MartyIX
  • 27,828
  • 29
  • 136
  • 207
dlock
  • 9,447
  • 9
  • 47
  • 67
4

Try this.

Properties config = new Properties();
config.setProperty("open_mode", "1");  //1 == readonly
Connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db", config);
szcoder
  • 1,396
  • 11
  • 9
2

Just want to add that when using HikariCP, you need two datasource properties:

readOnly=true
dataSource.open_mode=1
rustyx
  • 80,671
  • 25
  • 200
  • 267
1

I had a slightly different scenario, but the same issue. If you have a spring boot service using Hikari you need to provide 2 configuration settings in your application.properties

spring.datasource.url=jdbc:sqlite:myDatabase.db?open_mode=1
spring.datasource.hikari.read-only=true
hecko84
  • 1,224
  • 1
  • 16
  • 29
0

I came across your post and this is what helped me: I made a connection similar to the one listed here http://www.shoaibinamdar.in/blog/?p=313 but insread of

    ds.setUrl("jdbc:sqlite::resource:"+
    getClass().getResource("sample.db").toString());

I put

    ds.setUrl("jdbc:sqlite:resource/sample.db");

the sample.db went in /myprojectname/resource/sample.db, with resource being in the same dir (myprojectname) as /myprojectname/src/

Hope this helps someone

tricknology
  • 1,092
  • 1
  • 15
  • 27