I’m trying to create a mysql table from a thesaurus text file I got off project gutenberg; however, I’m having some issues doing it. I was able to write the following script which successfully creates a table with the information:
LOAD DATA LOCAL INFILE 'C:/mthesaur.txt'
INTO TABLE thesaurus_entries
COLUMNS
TERMINATED BY ','
LINES
TERMINATED BY '\r\n'
In the text file, the first word is the thesaurus entry, and it is followed by all its synonyms separated by commas. Each entry is separated by a line break.
My problem is that I was originally going to use just one table with a large number of columns, but I can’t figure out how to easily make 100+ columns for the synonyms. I tried doing this:
DELIMITER $$
CREATE PROCEDURE ABC()
BEGIN
DECLARE a INT Default 0 ;
simple_loop: LOOP
SET a=a+1;
ALTER TABLE `thesaurus`.`thesaurus_entries` ADD COLUMN a
VARCHAR(60) NULL AFTER `thesaurus_entries_id` ;
IF a=100 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END $$
But all that did was add one column named “a” instead of adding 100 columns named “1, 2, 3, etc.”
If it were easy to make the table as a key value pair (with separate tables for each word’s synomyms), I’d be willing to go that route, but I can’t figure out how to do that from a txt file.
Thank you for your help!
You probably don’t want a table with hundreds of columns. Instead, you probably want just two columns,
wordandsynonym. Each row in your input file will generate (potentially) multiple rows in your table.You could write a simple preprocessor (in your favorite scripting language) to convert your input file to a list of word,synonym pairs. Then load it as you are doing now.