1

我是新手,java脚本,mysql的东西。我在 mirth 中设置了一个通道来读取文本文件并将其转换为 xml。它工作正常。我还尝试使用另一个通道中的数据库编写器将 xml 发送到 mysql 数据库。

这就是 javascript 代码的样子

var dbConn = DatabaseConnectionFactory.createDatabaseConnection('com.mysql.jdbc.Driver','jdbc:mysql://192.168.1.4:3306/mirth','root','');
var result = dbConn.executeUpdate('INSERT INTO jon (xml) values ('1234')');
dbConn.close();

上面的代码在 jon 表中插入一条值为 '1234' 的记录。但是如何将我通过 source:channel reader 读取的 xml 发送到数据库?我试图用 ('+messageObject.getEncodedData()+) 或 rawdata 或 transformeddata 替换 '1234'。他们都没有工作。我收到以下错误:

ERROR (org.mule.impl.DefaultComponentExceptionStrategy:95) .... Wrapped com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<?xml version="1.0" encoding="UTF-8"?><delimited><row><column1>1234</column1><co' at line 1 (1cf6717f-4818-4b18-acb2-3b93079f2e95#7) .....

我的意图是在一个字段中编写整个 xml,还不需要解析。谢谢你的病人。詹莫罕默迪

4

1 回答 1

0

The error you are getting makes it seem like this is a simple syntax issue. Forget Mirth for a second. In general, if you are inserting values into a character field in a db, you need single quotes around the value that you insert

INSERT INTO jon (xml) values ('1234') //single quotes around 1234

When you create a query in javascript in Mirth, you are basically replicating the same query that you would use in mysql--but you are building it as a string programatically. You basically build the following string and tell javascript to execute it:

"INSERT INTO jon (xml) values ('1234')" //single quotes around 1234

That means that if you have an XML mapped variable, you need to surround it by single quotes in the query string so that your DB knows the value you are trying to insert. Based on your error message, that seems to be the problem. If you post the actual code you are using to do the insert, I can check to see.

    var xmlVar = $('XmlVar');  //fill a variable
    var query = "INSERT INTO testMRN (sendingfacility) VALUES ('" + xmlVar + "')";  //notice the quotes around the variable 
    var update= dbConn.executeUpdate(query);
于 2012-08-01T14:56:14.410 回答