创建一个mysql自定义函数来从txt文件导入数据

时间:2014-09-28 11:07:00

标签: mysql sqlyog

我想创建一个mysql函数,它应该将数据从txt文件导入mysql表。

这是我到目前为止所做的:

打开创建新函数,命名函数load_file_txt 然后我添加了这些行

LOAD DATA INFILE `data_file.txt`
INTO TABLE `reservation_general`
FIELDS TERMINATED BY `,`
LINES TERMINATED BY `\n`

这样整个功能现在就像这样

DELIMITER $$

CREATE
    FUNCTION `customsearch`.`load_file_txt`()
    RETURNS TYPE
    BEGIN

LOAD DATA INFILE `data_file.txt`
INTO TABLE `reservation_general`
FIELDS TERMINATED BY `,`
LINES TERMINATED BY `\n`
    END$$

DELIMITER ;

我使用SQL Yog也安装了mysql workbench。 以下是txt文件中的一些数据。

0806,,Adolfo,Castro,CHECKED IN,5123551,5262313,11.09.14,01.12.14,XLOG,0,,
0807,Mr.,Matthew,Keehn,CHECKED IN,4942796,5091556,02.08.14,23.10.14,XBUG,1,,
0808,,Gregory,Jamison,CHECKED IN,5003068,5143829,15.08.14,15.10.14,XLOG,4,,
0901,Mr.,Mohan,Raj,CHECKED IN,4909299,5056058,24.07.14,24.10.14,XKLG,4,,
0901,Mr.,Asif,Azmi,CHECKED IN,4909305,5056066,24.07.14,24.10.14,XKLG,0,,
0901,Mr.,Sriram,Ramamoorthy,CHECKED IN,4909304,5056065,24.07.14,24.10.14,XKLG,0,,
0901,Mr.,Vimmal,Thundiyil,CHECKED IN,4909302,5056063,24.07.14,24.10.14,XKLG,0,,
0904,Mr.,Nasser,AL Khater,CHECKED IN,5187812,5325323,25.09.14,27.09.14,XOTG,2,0803,
0905,,Zoe,Treble,CHECKED IN,5003063,5143824,15.08.14,15.10.14,XLOG,1,,
0906,Mr.,Timo,Hornacek,CHECKED IN,4990560,5133071,06.08.14,29.09.14,XLOG,0,,
0906,,Urban,Petra,CHECKED IN,4990561,5133072,06.08.14,29.09.14,XLOG,0,,
0906,Mr.,Lauris,Coucaud,CHECKED IN,4990556,5133067,06.08.14,29.09.14,XLOG,0,0801,
0906,Mr.,Viliam,Fercak,CHECKED IN,4990559,5133070,06.08.14,29.09.14,XLOG,0,,
0906,,Jakub,Novotny,CHECKED IN,4990558,5133069,06.08.14,29.09.14,XLOG,0,,
0906,Mr.,Faria,Rodrigo,CHECKED IN,4990555,5133066,06.08.14,29.09.14,XLOG,0,,
0906,Mr.,Yannis,Michalandos,CHECKED IN,4968820,5113842,02.08.14,29.09.14,XLOG,6,0908,
0907,Mr.,Keoki,Sears,CHECKED IN,4878044,5024305,04.08.14,29.10.14,XBUG,1,,
0908,Mr.,Zacharias,Vailakis,CHECKED IN,4704796,4858307,07.08.14,07.11.14,XLOG,2,0903,
1001,Ms,Shahla,Jassim,CHECKED IN,5003076,5143837,15.08.14,15.10.14,XKLG,5,,
1002,Mr.,Pedro Luis,Gonzalez,CHECKED IN,4845808,4994067,12.09.14,12.12.14,T22G,2,1002,
1003,Dr,Hadi,Abderrahim,CHECKED IN,4918568,5066577,12.07.14,12.10.14,XOTG,1,1003,
1004,Ms,Aisha,Siyam,DUE OUT,5191796,5328567,25.09.14,26.09.14,XOTG,1,,
1005,Mr.,William Krik,Brooks,CHECKED IN,5003297,5143844,15.08.14,15.10.14,XLOG,2,,
1006,Miss,Tatenda,Mutsonziwa,CHECKED IN,5084546,5223307,01.09.14,02.12.14,XLOG,0,1006,
1006,Mr.,Vlad,Mihovilovic,CHECKED IN,4995048,5137059,01.09.14,02.12.14,XLOG,3,,
1006,Mr.,Vishal,Rander,CHECKED IN,5084552,5223313,01.09.14,02.12.14,XLOG,0,4403,
1007,Mr.,Robert Leonard,Costello,CHECKED IN,5035052,5174565,28.08.14,28.11.14,XBUG,1,1007,
1008,Mr.,Yousif Bani,Yassen,CHECKED IN,5033309,5172314,01.09.14,01.12.14,XLOG,1,1008,
1008,,TBA,Qatar University 04,CHECKED IN,4952550,5101560,01.09.14,01.12.14,XLOG,3,1008,
1101,,Klara,Steinwender,CHECKED IN,4994553,5136815,07.08.14,29.09.14,XKLG,1,,
1101,,Teeranpong,Jaro,CHECKED IN,4994552,5136814,07.08.14,29.09.14,XKLG,1,,
1101,Mr.,Yannis,Michalandos,CHECKED IN,4919806,5068064,09.07.14,29.09.14,XKLG,1,0908,
1101,,Luca,Luca,CHECKED IN,4923310,5071319,10.07.14,29.09.14,XKLG,0,,
1102,Mr.,Martin,Marks,CHECKED IN,4896312,5042072,30.06.14,30.09.14,T22G,2,1102,
1103,Mr.,Jonathan,Collier,CHECKED IN,5123806,5262564,26.09.14,10.10.14,XOTG,1,1103,
1104,Mr.,Ryan,Bowd,CHECKED IN,4922299,5070311,31.07.14,31.10.14,XOTG,1,1104,
1105,,TBA,Qatar University 05,CHECKED IN,4862801,5010813,01.09.14,01.12.14,XLOG,3,1105,
1106,Ms,Wendy,Isaacs,CHECKED IN,5003066,5143827,15.08.14,15.10.14,XLOG,4,,
1107,Mr.,Fakhri,Nawahda,CHECKED IN,4938054,5087066,02.08.14,02.10.14,XBUG,1,1107,
1108,Mr.,Nicolas,Renna,CHECKED IN,5166547,5305058,21.09.14,30.09.14,XLOG,0,,
1108,Mr.,Ali,Haidary,CHECKED IN,5166546,5305057,21.09.14,30.09.14,XLOG,3,1608,

尝试运行以上功能后,我收到此错误。

Query: CREATE FUNCTION `customsearch`.`load_file_txt`() RETURNS TYPE BEGIN LOAD DATA INFILE `data_file.txt` INTO TABLE `reservation_gen...

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE
    BEGIN
LOAD DATA INFILE `data_file.txt`
INTO TABLE `reservation_general`' at line 3

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0 sec

我不知道为什么我会收到此错误。希望是否有人可以帮助我创建函数来从文件中读取数据并将其导入到mysql表中。

0 个答案:

没有答案