【问题标题】:Sqlite - how to escape double quotes?Sqlite - 如何转义双引号?
【发布时间】:2015-01-14 19:46:23
【问题描述】:

我在导入 sql lite 时遇到问题。 我正在将 Sql Server 中的表导出到以 UTF-8 编码的平面文件中。然后尝试将平面文件导入 sqlite db。 DB 采用 UTF-8 编码。

这些行很麻烦(制表符分隔,行以 CRLF 结尾):

ID  w   posid   def
1234    bracket 40  "(" and ")" spec...

1234    bracket 40  Any of the characters "(", ")", "[", "]", "{", "}", and, in the area of computer languages, "<" and ">".

错误:

未转义的 " 字符

我试过用双引号“”替换引号“,还是不行。

导入设置:制表符分隔符

.分隔符“”

.import data.txt 单词

sqlite 表架构:

CREATE TABLE words (ID integer NOT NULL, w TEXT  NOT NULL, posid integer NOT NULL, def TEXT NOT NULL);

更新: 不知何故,在 Sql Server 的 def 字段的开头添加了一个哈希值:

更新单词 set def = '#' + def

不知道为什么会这样。这行得通,但它在字段中添加了一个不需要的字符。

【问题讨论】:

  • CSV 不可靠。不能导出到 SQL 语句吗?
  • 你可以试试:\"

标签: sqlite


【解决方案1】:

事实证明,当有换行符、引号或逗号时,导入可能会出错。

一种解决方案是将这些字符替换为一些其他字符序列或字符代码(例如 char(1)、char(2)...),并确保字段不包含这些序列或代码,之前你运行导入。例如,将引号替换为 --,然后导入,然后再次将 -- 替换为引号。我有另一个表格,其中包含一些带有换行符的文本字段,这个解决方案似乎有效。

before import:

update [table] set comment = REPLACE(comment, CHAR(13), '-*-')
update [table] set comment = REPLACE(comment, CHAR(10), '%-$-%')
update [table] set comment = REPLACE(comment, '"', '%-&-%')

after import:

update [table] set comment = REPLACE(comment, '-*-', CHAR(13))
update [table] set comment = REPLACE(comment, '%-$-%', CHAR(10))
update [table] set comment = REPLACE(comment, '%-&-%', '"')

【讨论】:

    【解决方案2】:

    要在不更改输入数据的情况下执行此操作,请使用 ascii 模式并将列分隔符设置为制表符,将行分隔符设置为 CRLF。

    .mode ascii
    .separator "\t" "\r\n"
    

    请参阅我的回答 to this other question 了解原因。

    【讨论】:

    • .mode tabs 失败时为我工作
    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多