Inline BLOB / BINARY data types in SQL / JDBC

≯℡__Kan透↙ 提交于 2019-12-09 04:21:13

问题


Let's say I want to avoid using bind variables in JDBC and run SQL using "ad-hoc" statements, e.g:

connection.createStatement().executeQuery("SELECT ...");

Is there any convention / JDBC escape syntax to inline BLOB data types? I know that H2 has this syntax:

INSERT INTO lob_table VALUES (X'01FF');

But that's not a standard. Any general solutions? Note, I'm interested in a general approach. I know that this can turn out to be terribly inefficient.


回答1:


There probably isn't a JDBC escape syntax, so I searched around a bit and found and successfully tested the following:

  • SQL Server, Sybase ASE, Sybase SQL Anywhere

    INSERT INTO lob_table VALUES (0x01FF);
    
  • DB2

    -- Use a blob constructor. This is not needed for VARCHAR FOR BIT DATA types
    INSERT INTO lob_table VALUES (blob(X'01FF'));
    
  • Derby, H2, HSQLDB, Ingres, MySQL, SQLite

    INSERT INTO lob_table VALUES (X'01FF');
    
  • Oracle

    -- As mentioned by a_horse_with_no_name, keep in mind the relatively low
    -- limitation of Oracle's VARCHAR types to hold only 4000 bytes!
    INSERT INTO lob_table VALUES (hextoraw('01FF'));
    
  • Postgres

    -- There is also hex encoding as of Postgres 9.0
    -- The explicit cast is important, though
    INSERT INTO lob_table VALUES (E'\\001\\377'::bytea);
    

    See A.H.'s answer for more details about Postgres' hex encoding

  • SQL Standard

    -- SQL actually defines binary literals as such 
    -- (as implemented by DB2, Derby, H2, HSQLDB, Ingres, MySQL, SQLite):
    <binary string literal> ::=
      X <quote> [ <space>... ] 
      [ { <hexit> [ <space>... ] <hexit> [ <space>... ] }... ] <quote>
    
    <hexit> ::=
      <digit> | A | B | C | D | E | F | a | b | c | d | e | f
    



回答2:


I'd like to add some PostgreSQL specific stuff to Lukas' answer:

The shortest and most easiest solution would be (since PostgreSQL 9.0 at least):

insert into lob_table (data) values( E'\\x0102030405FF' )

without any cast (if the column is already a bytea one) and only one \\x mark right at the beginning. This is the "hex format" documented in the section Binary Data Types.

Regarding the X'01FF' syntax: According to the string constant documentation PostgreSQL does support it - for bit strings. And it seems, that there is no standard conversion from bit to bytea.




回答3:


public String binaryLiteral(Connection con, byte[] bytes) {
    String databaseName = con.getMetaData().getDatabaseProductName();
    String binary = DatatypeConverter.printHexBinary(bytes);
    switch(databaseName) {
        case "Microsoft SQL Server":
        case "ASE": // Sybase
        case "Adaptive Server Enterprise": // Sybase
            return "CONVERT(VARBINARY(MAX), '0x" + binary + "', 1)";
        case "Oracle":
            return "HEXTORAW('" + binary + "')";
        case "PostgreSQL":
            return "E'\\\\x" + binary + "'";
        case "DB2":
            return "blob(X'" + binary + "')";
        default:
            // SQL Standard (Derby, H2, HSQLDB, Ingres, MySQL, SQLite)
            return "X'" + binary + "'";
    }
}


来源:https://stackoverflow.com/questions/9320200/inline-blob-binary-data-types-in-sql-jdbc

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