You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQLite 3.38.2中提取XML标签的elapsed数值?

在SQLite 3.38.2中提取XML属性elapsed的数值

由于SQLite 3.38.2没有原生XML解析功能,我们可以通过字符串定位函数的组合来提取elapsed属性的数值,以下是两种实现方式:

基础提取(返回整数类型)

直接提取elapsed的数值并转换为整数:

with cte(xml_tag) as (values 
  ('<Event time="Sat Apr 22 1:01:51.887" type="Debug" thread="2164: Main CIM worker thread" elapsed="1" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.883" type="Debug" thread="2164: Main CIM worker thread" elapsed="23" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.874" type="Debug" thread="2164: Main CIM worker thread" elapsed="456" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.846" type="Debug" thread="2164: Main CIM worker thread" elapsed="7890" function="Geodatabase.Cursor" code="EndCursor">'))
select 
  cast(
    substr(
      xml_tag, 
      instr(xml_tag, 'elapsed="') + 9, 
      instr(substr(xml_tag, instr(xml_tag, 'elapsed="') + 9), '"') - 1
    ) as integer
  ) as elapsed
from cte;

格式化输出(匹配示例右对齐样式)

如果需要和示例一致的右对齐格式,可使用printf函数做格式化:

with cte(xml_tag) as (values 
  ('<Event time="Sat Apr 22 1:01:51.887" type="Debug" thread="2164: Main CIM worker thread" elapsed="1" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.883" type="Debug" thread="2164: Main CIM worker thread" elapsed="23" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.874" type="Debug" thread="2164: Main CIM worker thread" elapsed="456" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.846" type="Debug" thread="2164: Main CIM worker thread" elapsed="7890" function="Geodatabase.Cursor" code="EndCursor">'))
select 
  printf('%6d', 
    cast(
      substr(
        xml_tag, 
        instr(xml_tag, 'elapsed="') + 9, 
        instr(substr(xml_tag, instr(xml_tag, 'elapsed="') + 9), '"') - 1
      ) as integer
    )
  ) as elapsed
from cte;

关键函数说明

  • instr(xml_tag, 'elapsed="'):定位elapsed="的起始位置,加上9(该字符串的字符长度)得到数值的起始索引
  • instr(substr(xml_tag, ...), '"'):从数值起始位置开始,找到下一个双引号的位置,确定数值的结束索引
  • substr:根据起始、结束索引截取目标数值字符串
  • cast(...) as integer:将截取的字符串转换为整数类型,确保数值的正确性
  • printf('%6d', ...):将整数格式化为宽度为6的右对齐字符串,匹配示例的输出样式

内容的提问来源于stack exchange,提问作者User1974

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:32:42