将gcsql中作为blob存储的svg图像转换为apps脚本中的字符串

woobm2wo  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(405)

我需要将svg图像存储在googlecloudsql(mysql)表中,然后提取它们,以便使用xml服务修改apps脚本中的一些xml属性值。图像存储在驱动器中。首先,我试图将图像作为字符串插入数据库,但收到一条错误消息:“argument is too large”。因此,我尝试了同样的方法,但将图像作为blob发送,这显然解决了这个问题,但是当我执行查询以提取blob,然后将它们转换为字符串时,我收到一个错误:“service error:jdbcblob”。我的代码在下面,你知道是什么导致了这个错误吗?我已经阅读了文档,但找不到解决方法。

function xmlRead(){    

       var id = "some id";
       var rawXml = DriveApp.getFileById(id).getBlob();

       var params = {
          ip: "some ip",
          user: "some user",
          password: "some password",
          database: "some db"
       }

       var dbUrl = 'jdbc:mysql://' + params.ip + '/' + params.database;
       var conn = Jdbc.getConnection(dbUrl, params.user, params.password);
       var stmt = conn.createStatement();

       var sql  = "INSERT INTO pruebas_conexion_blob (datos) VALUES ('" + rawXml + "');";
       var results = stmt.executeUpdate(sql);

       var sql_blob  = "SELECT * FROM pruebas_conexion_blob WHERE id = (SELECT MAX(id) FROM pruebas_conexion_blob)";  
       var results = stmt.executeQuery(sql_blob); //type jdbcresultset

       while (results.next()) { 
            var results_blob = 
            results.getBlob('datos').getAppsScriptBlob().getDataAsString(); //error happens on this line
        }

       var xml = XmlService.parse(results_blob);
       return xml; 
   }

顺便说一下,这是我在stackoverflow上的第一篇帖子,如果我违反了规则,请告诉我。

flseospp

flseospp1#

我解决了我的问题,代码如下:

function xmlRead(){    

      var id = "some id";
      var rawXml = DriveApp.getFileById(id).getBlob().getBytes();

      var params = {
         ip: "some ip",
         user: "some user",
         password: "some password",
         database: "some db"
      }  

      var dbUrl = 'jdbc:mysql://' + params.ip + '/' + params.database;
      var conn = Jdbc.getConnection(dbUrl, params.user, params.password);
      var stmt = conn.createStatement();

      var statement = conn.prepareStatement("INSERT INTO pruebas_conexion_blob (datos) VALUES (?)");
      statement.setBytes(1, rawXml);
      var results = statement.executeUpdate();

      var consulta  = "SELECT datos FROM pruebas_conexion_blob WHERE id = (SELECT MAX(id) FROM pruebas_conexion_blob)";  
      results = stmt.executeQuery(consulta); 

      while (results.next()) { 
         var results_string = results.getString('datos');
     }  

     results.close();
     stmt.close();
     statement.close();

     var xml = XmlService.parse(results_string); 
     return xml; 
 }

相关问题