H2Databaseã使ç¨ãã¦ã¿ã
ãªãã¨ãªã軽éãã¼ã¿ãã¼ã¹ã使ç¨ããããªã£ãã®ã§ãããã£ã¦ã¿ãã
使ç¨ããã¾ã§ãä¸ç¬ãããã¦æä½ãç°¡åãçµã¿è¾¼ã¿ãç°¡åã¨é常ã«æ軽ã§ããã
H2Databaseã¨ã¯
H2 Databaseã¨ã¯ãJavaãã©ãããã©ã¼ã ä¸ã§åããACIDãªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ããªã¼ãã³ã½ã¼ã¹ãé度ãéããã¨ãç¹å¾´ã¨ãã¦ããã
http://ja.wikipedia.org/wiki/H2_Database
軽éãªãã¼ã¿ãã¼ã¹ã¨ããèªèã§ããã®ã ãããã
ä¼¼ããããªç¨éã ã¨ä»¥ä¸ã®Databaseããããããã
Groovyãã使ç¨
Grapeã使ç¨ããã°ç°¡åã
newInstanceã§ãmemããæå®ãããã¨ã§ã¡ã¢ãªä¸ã«ä½æãã¦ãã¾ãã
ããã«PATHãæå®ãããã¨ã§ä¿åããã¾ãã
@Grapes([ @Grab(group = 'com.h2database', module = 'h2', version = '1.3.173'), @GrabConfig(systemClassLoader = true) ]) import groovy.sql.Sql def sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver") sql.execute ''' create table project(id int, name varchar(10)) ''' sql.dataSet("project").add([id:1,name:"hoge"]) sql.dataSet("project").add([id:2,name:"huga"]) sql.rows("select * from project").each { println "id=${it.id}, name=${it.name}" }
ã¢ã¸ã¥ã¼ã«ãåå¾ãã¦ä½¿ç¨
ã¢ã¸ã¥ã¼ã«è½ã¨ãã¦ã解åãã¦å®è¡ããã°OK
- H2 Database Engineã®Downloadããzipã§åå¾
- 解å
- ãbin\h2.batãããbin/h2.shããå®è¡
- ãã°ã¤ã³ç»é¢ã表示ããã
- æ¥ç¶ãã¿ã³æ¼ä¸
- SQLå®è¡
ãã°ã¤ã³ç»é¢ã®ãjdbc URLãå
ã«ã~/testãã¨ãããã©ãããã¯ãã®PATHã«ãã¡ã¤ã«ä½ã£ã¦ä¿åããã¨ããäºã
ãããªé¢¨ã«ãtest.h2.dbãããtest.trace.dbãã®2ã¤ã®ãã¡ã¤ã«ãä½æããã¾ãã
C:\Users\yamap>dir /B test* test.h2.db test.trace.db
å°ãããã¹ãæ¥ç¶ããå®è¡ãã段éã§ãä½æããããããã®ã§æ³¨æã
DBãæ¢ãããå ´åã«ã¯ãã°ã¤ã³ç»é¢ â è¨å® â ã·ã£ãããã¦ã³
jdbcæ¥ç¶
ãbin/h2-1.3.173.jarããClassPathã«è¨å®ãã¦ããã°ã¤ã³ç»é¢ã«è¨å®ããã¦ããããã«æ¥ç¶ããã°æ¥ç¶å¯è½ã§ãã
import groovy.sql.Sql def sql = Sql.newInstance("jdbc:h2:c:/Users/yamap/test", "sa", "", "org.h2.Driver") sql.rows("select * from project").each { println "id=${it.id}, name=${it.name}" }
ãã ãã³ãã¯ã·ã§ã³ã¯1ã¤ï¼ããå¼µããªããããªã®ã§ã2ç®æç®ããæ¥ç¶ãããã¨ããã¨ä¸è¨ã®ãããªä¾å¤ãçºçãã¾ãã
ããã¾ã§ã1ã¤ã®ã¢ããªã±ã¼ã·ã§ã³ãã使ç¨ããã¨ããåæã®ããã§ãã
ERROR org.h2.jdbc.JdbcSQLException: ãã¼ã¿ãã¼ã¹ã使ç¨ä¸ã§ã: "Locked by another process".
csvãã¡ã¤ã«ã¨ã®é£æº
H2 Databseã¯csvãã¡ã¤ã«ã¨ã®é£æºæ¹æ³ã容æã§ãã
èªã¿è¾¼ãå ´åã¯ãcsvread('å
¥åãã¡ã¤ã«å')ããæ¸ãè¾¼ãå ´åã¯ãcall csvwrite('åºåãã¡ã¤ã«å', 'SQL')ãã§å¯è½ã
æ¸ãè¾¼ã¿
call csvwrite('out.csv', 'select * from project');
- out.csv
"ID","NAME" "1","hoge" "2","huga"
csvãã¡ã¤ã«ããã®ãã¼ã¿ã¤ã³ãã¼ã
insert into project select * from csvread('import.csv');