将具有特殊字符的SQL查询插入H2database时出错

时间:2013-04-30 07:57:19

标签: insert

我有一个xml,我解析它来读取节点的值,我将这些值输入到自定义列表中以进行验证。

现在在我尝试将值插入H2Database中的表后解析xml的值时,我最终得到了语法错误。

我解析的样本xml

<DmtTask xsi:type="SqlTask">
        <TaskSeverity></TaskSeverity>
      <TaskCategory></TaskCategory>
      <TaskTitle></TaskTitle>
      <TaskMessage></TaskMessage>
      <TaskCode></TaskCode>
          <DSqlGenerated></DSqlGenerated>
      <TaskName>VALIDATE_COMPANIES_MISSING_AVAILABILITY_DATE</TaskName>
      <SqlQuery>DROP TABLE CLIENT_VALIDATION.COMPANIES_MISSING_AVAILABILITY_DATE IF EXISTS;
            CREATE TABLE CLIENT_VALIDATION.COMPANIES_MISSING_AVAILABILITY_DATE AS
            (SELECT * FROM CLIENT.COMPANIES WHERE AVAILABILITY_DATE = '');
            </SqlQuery>
          <ReportError>SELECT 'Missing Availability Date' AS ERROR_DESC,
            ORGANIZATION_NAME,AVAILABILITY_DATE,COUNTRY,EMAIL_ADDRESS,INSTANT_MESSENGER_ADDRESS,REVERSE_DEBIT_CREDIT,KEEP_DEBIT_CREDIT_AND_REVERSE_SIGN
            FROM CLIENT_VALIDATION.COMPANIES_MISSING_AVAILABILITY_DATE</ReportError>
      <MetricSchemaName>DMTSCRIPTS</MetricSchemaName>
    </DmtTask>

我写的动态查询来从后面的代码更新表。 TaskEditorText是自定义集合List

的对象
for (int i = 0; i < taskEditorText.Count; i++)
                        {
                            //string sqlGenerated = "false";
                            db.ExecuteNonQuery("INSERT INTO " + Schema + ".DMTScriptTasks(DmtScriptId,TaskSeverity,TaskCategory,TaskTitle,TaskMessage,TaskCode,TaskName,Execute,UpdatedUser,UpdatedTime) VALUES('" + id + "','" + taskEditorText[i].TaskSeverity + "','" + taskEditorText[i].TaskCategory + "','" + taskEditorText[i].TaskTitle + "','" + taskEditorText[i].TaskMessage + "','" + taskEditorText[i].TaskCode + "','" + taskEditorText[i].TaskName + "','" + true + "','" + CreatedUser + "','" + CreatedDate + "');");
                        }

导致语法错误的示例值

DROP TABLE CLIENT_VALIDATION.COMPANIES_INVAL_AVAILABILITY_DATE IF EXISTS;
        CREATE TABLE CLIENT_VALIDATION.COMPANIES_INVAL_AVAILABILITY_DATE AS
            (SELECT * 
            FROM CLIENT.COMPANIES   
            WHERE
                AVAILABILITY_DATE != ''
            AND
                AVAILABILITY_DATE NOT LIKE '____-__-__');

我尝试替换特殊字符,编写正则表达式,使用Regex.Escape没有任何效果。我对xml解析没有任何控制权。即使我是H2Database的新手。因此,如果有人知道将这种类型的值插入数据库中的列的方法,请你帮助我。

这里我们使用的数据库是H2DataBase(Java数据库)。

1 个答案:

答案 0 :(得分:0)

PreparedStatement prep = reader.Connection.prepareStatement("INSERT INTO " + Schema + ".DMTScriptTasks(DmtScriptId,TaskSeverity,TaskCategory,TaskTitle,TaskMessage,TaskCode,TaskName,Execute,UpdatedUser,UpdatedTime,SqlQuery) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
Then loop through the collection

for (int i = 0; i < taskEditorText.Count; i++)
                        {
                            prep.setString(1, id);
                            prep.setString(2, taskEditorText[i].TaskSeverity);
                            prep.setString(3, taskEditorText[i].TaskCategory);
                            prep.setString(4, taskEditorText[i].TaskTitle);
                            prep.setString(5, taskEditorText[i].TaskMessage);
                            prep.setString(6, taskEditorText[i].TaskCode);
                            prep.setString(7, taskEditorText[i].TaskName);
                            prep.setString(8, "true");
                            prep.setString(9, CreatedUser);
                            prep.setString(10, CreatedDate);
                            prep.setString(11, taskEditorText[i].SqlQuery);
                            //batch insert
                            prep.addBatch();
                            reader.Connection.setAutoCommit(false);
                            prep.executeBatch();
                            reader.Connection.setAutoCommit(true);

                        }

这里taskEditorText是一个List,我试图插入值的数据库是H2Database。

然后我进行了插件查询的批量更新。