0

I have a database with 6 columns and several thousand rows in which I'd like to export the contents into separate, named text files.

id  title   text                text_2
1   blah    lorem ipsem...      indigo violet...
2   gunf    ipsem lorem...      up down left...
3   faff    sir I have a...     amarillo albuquerque...

I'd like to create the following text files for each row:

filename = id - title.txt; content = title

filename = id - title.txt; content = text

filename = id - title.txt; content = text_2

I've looked and can't think how to do it. I used to use a macro in Excel to convert cells to txt but the text is too big for Excel's cell character limit.

I'm using SQLite but am not wedded to it (though would rather not have to buy a program if I can avoid it).

Any advice on what to do? While not too techy I can follow some basic code.

1
  • You're saying that for each row you want to create the same file 3 times, but with different contents. Can you be more precise? Aren't those suppose to be 3 different files? Or is it single file with 3 lines of contents?
    – Googie
    Feb 12, 2015 at 9:57
0

You can use SQLiteStudio 3.x.x and it's custom SQL functions.

Open "Custom SQL Functions editor" (the one with "fx" icon) and add new function, let's call it saveToFile. Select Tcl as implementation language (it's in the top-right corner), leave "Type" as Scalar. If you want you can define input arguments (it's not mandatory, it's just so the code assistant will help you later on when invoking this function): fileName and contents. It's also okay to keep "Register in all databases" option.

Now the most important thing - enter following implementation code:

if {[catch {
  lassign $argv fileName contents
  set fd [open "C:/tmp/a/$fileName" a+]
  puts $fd $contents
  close $fd
} res]} {
  return "error: $res"
} else {
  return "ok"
}

The code contains C:/tmp/a which is path to directory where your files will be created. Change it to whatever you want. The directory must exist.

Commit your function (commit button is on top of functions editor).

Now open SQL editor window (the one with blank paper and a pencil) and type query like this:

SELECT saveToFile(id || ' - ' || title || '.txt', title || x'0a' || text || x'0a' || text_2) AS result FROM table_name;

This query will create one file per row and will put title, text and text_2 columns as 3 separate lines in the file.

The query will return for each row status ok when there was no problem, or error: ... with error details when there was problem, for example with creating a file.

1
  • Thank you! You're right, it is 3 different files with different content. The code worked for me, I adapted the SQL editor query to just include title in the contents and it worked. IT doesn't like odd characters in the title (eg, '/') when making the file so I just told it to URL encode it then rename the files after. Not pretty but does the job.
    – JRUK
    Feb 13, 2015 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.