1

I have a database of entries consisting of a 'name', 'id' and a 'description', but currently the 'description' field is set to the file path of a .txt file that actually contains the description content. Each .txt file's name is each row's 'id', plus the .txt extension and they all reside in the same directory.

Can I load and replace each 'description' field with the content from the relevant text file (using MySQL)?

2 Answers 2

1

You can't write a MySQL query directly that will read the description values from your file system. That would require the MySQL server to be able to read raw text from files in your file system. You Can't Do That™.

You certainly can write a program in your favorite host language (php, java, PERL, you name it) to read the rows from your database, and update your description rows.

You could maybe contrive to issue a LOAD DATA INFILE command to read each text file. But the text files would have to be very carefully formatted to resemble CSV or TSV files.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, this is not entirely true.
1

Purely using mysql this would be a difficult, if not impossible exercise because mysql does not really offer any means to open files.

The only way to open an external text file from mysql is to use LOAD DATA INFILE command, which imports the text file into a mysql table. What you can do is to write a stored procedure in mysql that:

  1. Create a temporary table with a description field large enough to accommodate all descriptions.
  2. Reads all id and description field contents into a cursor from your base table.
  3. Loop through the cursor and use load data infile to load the given text file's data into your temporary table. This is where things can go wrong. The account under which mysql daemon / service runs needs to have access to the directories and fiels where the description files are stored. You also need to be able to parametrise the load data infile command to read the full contents of the text file into a single field, so you need to set the field and line terminated by parameters to such values that cannot be found in any of the description files. But, even for this you need to use a native UDF (user defined function) that can execute command line programs because running load data infile directly from stored procedures is not allowed.

See Using LOAD DATA INFILE with Stored Procedure Workaround-MySQL for full description how to this.

  1. Issue an update statement using the id from the cursor to update the description field in your base table from the temporary table.
  2. Delete the record from your temp table.
  3. Go to 3.

It may be a lot easier to achieve this from an external programming language, that has better file manipulation functions and can update each record in your base table accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.