java + sqlite: how to open database as read-only?

前端 未结 4 1392
南旧
南旧 2020-12-20 17:45

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?

相关标签:
4条回答
  • 2020-12-20 17:56

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

    readOnly=true
    dataSource.open_mode=1
    
    0 讨论(0)
  • 2020-12-20 17:57

    Try this.

    Properties config = new Properties();
    config.setProperty("open_mode", "1");  //1 == readonly
    Connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db", config);
    
    0 讨论(0)
  • 2020-12-20 17:58

    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

    0 讨论(0)
  • 2020-12-20 18:06

    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());
    
    0 讨论(0)
提交回复
热议问题