Jump to content

[SOLVED] .txt to MySQL import problem


quillspirit

Recommended Posts

I have over 30,000 product descriptions that I need to import into my database. These descriptions are currently in individual .txt files (Named for the item_id) such as 10004.txt

 

I managed to merge them all into one single .txt file - here is a sample: Note that all chars are represented in the descriptions: , " / - ; () [] {} | etc.

 

23018 14K gold tubular hoop earrings, (3mm tube, 1 3/16" diameter).  Pair

23019 14K gold tubular hoop earrings, (2mm tube, 3/4" diameter).  Pair

23047 14K gold cultured pearl diamond ring. (6 x 10mm, 1.5 points TDW).

23059 14K gold cultured pearl heart post earrings, (4.5-5 mm). Pair

23064 14K gold Figaro necklace, (1mm).

23065 14K gold 18" twisted serpentine necklace, (.5mm).

23067 14K gold 18" box necklace, (.5mm).

23069 14K gold serpentine necklace, (.75mm).

23073 14K gold box necklace (.5mm) with hearts (6 x 6mm).

23074 14K gold box necklace, (.5mm).

23075 14K gold diamond cut Boston cable necklace, (.5 mm).

23077 14K gold twisted Singapore necklace, (1 mm).

 

How can I make this .txt file importable into MySQL database? I need to drop it into an empty column, between the rest of the data that is already in there. (MAKING SURE it matches up with the right item_num)

 

 

item_num  item_page_num  item_name  item_description  item_units  item_price1 

x                   x                     x                     ???                      x              x

 

 

Right now, I have my scripting setup to include the needed .txt description to display with the other product info, but that just isn't very good. I want the descriptions to be searchable, and editable in the database.Another bonus would be if I could trim off the item ID numbers before each description, but some of them are alphanumeric or have a space. That is a project for another day.

 

Thanks for any help!

 

 

Link to comment
Share on other sites

Yes... example of what you mean by regular expression.

 

Unix with:

 

PHP version 4.4.2

MySQL version 4.0.27-standard

 

 

btw, I do know how to import CSV from Excel... but I can't figure out how to break this up into rows into Excel.

Link to comment
Share on other sites

With the code block of data from your original post opened in vim (a text editor) and the following regular expression command

 

:%s/^\([0-9]*\)\s\(.*\)/'\1','\2'/g

 

will result in each line quoted as so:

 

'23018','14K gold tubular hoop earrings, (3mm tube, 1 3/16" diameter).  Pair'

'23019','14K gold tubular hoop earrings, (2mm tube, 3/4" diameter).  Pair'

'23047','14K gold cultured pearl diamond ring. (6 x 10mm, 1.5 points TDW).'

'23059','14K gold cultured pearl heart post earrings, (4.5-5 mm). Pair'

'23064','14K gold Figaro necklace, (1mm).'

'23065','14K gold 18" twisted serpentine necklace, (.5mm). '

'23067','14K gold 18" box necklace, (.5mm). '

'23069','14K gold serpentine necklace, (.75mm).'

'23073','14K gold box necklace (.5mm) with hearts (6 x 6mm).'

'23074','14K gold box necklace, (.5mm).'

'23075','14K gold diamond cut Boston cable necklace, (.5 mm).'

'23077','14K gold twisted Singapore necklace, (1 mm).'

 

You may need to run a quick regular expression to addslashes to single quotes before wrapping the id and description in quotes.

 

EDIT: the regular expression is playing havoc with the bbcode

Link to comment
Share on other sites

I suppose so.. but I am not highly experienced with csv import/exporting.

 

Besides, opening the file in vim and executing the regexp:

 

%s/^\([0-9]*\)\s\(.*\)/('\1','\2'),/g

 

then replacing the last comma with a semicolon, appending the entire string with INSERT INTO tbl_name (id, description) VALUES would result in a SQL query that you could execute.

Link to comment
Share on other sites

1. open vim

2. open the text file

3. type : (typing the colon should drop vim's focus the command line)

4. type %s/^\([0-9]*\)\s\(.*\)/('\1','\2'),/g

5. hit the enter key

5. swap the last comma in the text file with a semicolon

6. add the INSERT statement to the front of the entire string

 

.... and and you have yourself a SQL query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.