H2 Java Insert ignore - allow exception

前端 未结 1 1624
抹茶落季
抹茶落季 2021-01-18 11:18

I am working on a java plugin interfacing with an H2 database. What I really want is an \"Insert Ignore\" statement; however, I\'m aware that H2 doesn\'t support this. I am

相关标签:
1条回答
  • 2021-01-18 11:34

    One solution is to use:

    insert into test 
    select 1, 'Hello' from dual 
    where not exists(select * from test where id = 1)
    

    This should work for all databases (except for the dual part; you may need to create your own dummy table with one row).

    To disable logging exceptions, append ;trace_level_file=0 to the database URL:

    jdbc:h2:~/test;trace_level_file=0
    

    or run the SQL statement:

    set trace_level_file 0
    
    0 讨论(0)
提交回复
热议问题