【问题标题】:how to import .txt to mysql file?如何将 .txt 导入 mysql 文件?
【发布时间】:2018-01-30 20:13:21
【问题描述】:

如何将.txt导入mysql文件?

我有一个包含数百条记录的 .txt 文件,我想将它导入到 mysql 数据库表中。

示例: word.txt 包含数据:

word_1
word_2
word_3
.
.
.
.
.
word_n

我在 mysql 数据库中有一个表: tbl_word:

field: id_word, value_word, create_date, update_date

word.txt 文件每一行中的值我将插入/导入列 value_word 并设置 id_word AUTO_INCREAMENT,怎么办?

【问题讨论】:

  • @Bradley-Coupland 是的,我使用 phpmyadmin

标签: mysql database


【解决方案1】:

选中LOAD DATA INFILE Syntax 将文件导入表中

load data infile 'word.txt'
into tbl_word
fields
  terminated by '\t'
  lines terminated by '\n'
ignore 0 lines
(@c1,@c2,@c3,..,@cN)
set value_word=@c1;

id_word 会自动递增

【讨论】:

  • 不,N 是要导入的文件的列数,如果 word.txt 有 1 列,那么您需要的是 (@c1)
  • error : #1064 - 您的 SQL 语法有错误;检查手册@guigoz 有错误:这对应于您的 MariaDB 服务器版本,以便在 'tbl_word 字段以 '\t' 终止的行附近使用正确的语法,以 '\n' 终止
  • 谢谢,您的 sintax 不完整,我补充说:INTO TABLE。完整的语法变为:将文件中的数据 'word.txt' 加载到 TABLE tbl_word 字段中以 '\t' 结尾的行以 '\n' 结尾忽略 0 行 (@c1,@c2,@c3,..,@cN) 设置 value_word =@c1;
【解决方案2】:

tbl_word _listData = new tbl_word();

string text = 从文本文件中读取所有文本。

for (iterate till end of text )
{
if(line break logic)
var value = split line;
_listData.value_word = value;
insert into table tbl_word values will have var value;
}

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多