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

…衆ロ難τιáo~ 提交于 2019-11-28 07:56:57

问题


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?


回答1:


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());



回答2:


Try this.

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



回答3:


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

readOnly=true
dataSource.open_mode=1



回答4:


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



来源:https://stackoverflow.com/questions/4574303/java-sqlite-how-to-open-database-as-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!