mysql - 将 XML 导入 mySQL 时列名无效?

标签 mysql xml database

我有一个 XML 文件,正在尝试将其导入到 mySQL 中。但是,由于变量名称中包含“-”,因此使用 LOAD XML 查询不起作用。如何将其导入数据库并处理无效字符?:

    <?xml version="1.0" encoding="UTF-8"?>
<projects type="array">
  <project>
    <active type="boolean">true</active>
    <bill-by>none</bill-by>
    <billable type="boolean">true</billable>
    <budget type="decimal" nil="true"/>
    <budget-by>none</budget-by>
    <client-id type="integer">999</client-id>
    <code></code>
    <cost-budget type="decimal" nil="true"/>
    <cost-budget-include-expenses type="boolean">false</cost-budget-include-expenses>
    <created-at type="datetime">2013-06-27T18:10:26Z</created-at>
    <fees type="decimal" nil="true"/>
    <highrise-deal-id type="integer" nil="true"/>
    <hourly-rate type="decimal" nil="true"/>
    <id type="integer">38125</id>
    <name>Banking</name>
    <notify-when-over-budget type="boolean">false</notify-when-over-budget>
    <over-budget-notification-percentage type="decimal">80.0</over-budget-notification-percentage>
    <over-budget-notified-at type="date" nil="true"/>
    <show-budget-to-all type="boolean">false</show-budget-to-all>
    <updated-at type="datetime">2013-06-27T18:13:10Z</updated-at>
    <estimate nil="true"/>
    <estimate-by>none</estimate-by>
    <notes></notes>
    <hint-earliest-record-at type="NilClass">2013-06-24</hint-earliest-record-at>
    <hint-latest-record-at type="NilClass">2013-06-30</hint-latest-record-at>
    <hint-earliest-record-at>2013-06-24</hint-earliest-record-at>
    <hint-latest-record-at>2013-06-30</hint-latest-record-at>
  </project>

最佳答案

您必须修复 XML 文件中的字段名称。

此外,您的 XML 文件还存在另一个问题: <hint-earliest-record-at><hint-latest-record-at>出现两次,因此您必须选择一个进行删除或重命名。

我尝试了一个实验:

  1. 创建一个表格,其中的列不包含破折号。
  2. 针对该表创建可更新的 VIEW,并使用破折号字符定义 View 的列名称
  3. 将 XML 加载到 VIEW 中,希望它插入到基表的相应列中。

但这没有用。它插入了一行,但所有值都作为 NULL 插入。

我还测试了创建一个具有正常列名称(不带破折号)的 VIEW,然后编辑了 XML 中的字段名称。通过 VIEW 加载此 XML 有效,并且行被插入到基表中。所以我知道 LOAD XML 可与可更新的 VIEW 一起使用。

关于mysql - 将 XML 导入 mySQL 时列名无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17622530/

相关文章:

php - Codeigniter 根据两个连接条件连接两个表

android - 删除主键SQLite后递减外键

xml - XSL 问题 : how can I select a set of nodes based on a set of node values?

Jquery:将 XML 节点映射到 HTML ID 元素

java - 使用 XSLT 或 : How to use an XML Template? 转换 "empty"XML

Java无法在数组列表中存储对象

mysql - 如何使数据库轮询可扩展?

c# - 无法将源类型 'string' 转换为目标类型 'uint' c# 中的错误

mysql - 在 innodb 中重置 auto_increment

java - Discord 机器人异常重复