SQLite的 - 从CSV文件中插入一个字符串换行到数据库 [英] SQLite - inserting a string with newlines into database from csv file

查看:146
本文介绍了SQLite的 - 从CSV文件中插入一个字符串换行到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我想很长的文本输入到一个SQLite数据库,在文本字段注入。本文中有新的生产线(即,它跨越了多个段落)。

I am trying to inject a long text entry into a SQLite database, in a TEXT field. This text has new lines in it (i.e. it spans multiple paragraphs).

我可以得到新线展现出来,如果​​我手动执行INSERT:

I can get the new lines to show up if I do the INSERT manually:

INSERT INTO "LOGENTRY" VALUES(5,40,'PLACE','line1
line2

line4
',1283990533315,'4A','TEXT','',NULL);

但是,如果我有一个CSV文件中的相应文本并试图.IMPORT入表中,我得到一个错误,它的预期比存在更多的列(如一旦遇到新行,则程序假定它的输入端,因此它缺少列)。

but if I have the equivalent text in a CSV file and attempt to .import it into the table, I get an error that it's expecting more columns than exist (as soon as the new line is encountered, the program assumes it's the end of the input and thus it's missing columns).

这可能吗?或者是手工做的每个INSERT的唯一途径?

Is this possible? Or is the only way to manually do each INSERT?

推荐答案

您的问题是csv格式:SQLite不支持包含换行符的值

Your problem is the csv format: sqlite doesn't support values containing newlines.

在SQLite的话:

  • 逗号分隔栏

  • Commas separate fields

双引号可用于组,其中包含逗号的字段。

Double quotes can be used to group a field which contains commas.

新线划定记录

New lines delimit records

您要么需要逃避/删除您新的生产线,或者使用其它更适合的文件格式。

You either need to escape/remove your new lines, or, use some other more suitable file format.

以下(SED)定期EX presion公司将删除不必要的换行

The following (sed) regular expresion will "delete" unwanted line feeds

s/\(,"[^",]*\)$/\1\1/g

这篇关于SQLite的 - 从CSV文件中插入一个字符串换行到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆