Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Modify Each Line Of SQL File
    primarykey
    data
    text
    <p>I need to modify many SQL statements to include an incremented number to keep track of each word in the 'paragraph' column. Sorry, but my PHP skills are limited. I am thinking a foreach loop after reading the file, but can't come up with anything to actually modify the lines to include each a value for each word.</p> <p>Current SQL:</p> <pre><code>INSERT INTO `book` VALUES(1, 1, 1, 'These are the words in a sentence in a chapter in a book.'); INSERT INTO `book` VALUES(1, 1, 2, 'These are the words in the second sentence in a chapter in a book.'); INSERT INTO `book` VALUES(1, 1, 3, 'These are the words in the third sentence in a chapter in a book.'); ...and so on. </code></pre> <p>The current VALUES are 'book', 'chapter, 'paragraph_number', 'paragraph'.</p> <p>Goal:</p> <pre><code>INSERT INTO `book` VALUES(1, 1, 1, 1,'These'); INSERT INTO `book` VALUES(1, 1, 1, 2,'are'); INSERT INTO `book` VALUES(1, 1, 1, 3,'the'); INSERT INTO `book` VALUES(1, 1, 1, 4,'words'); </code></pre> <p>The -new- VALUES are 'book', 'chapter, 'paragraph_number', 'paragraph', 'word_number'.</p> <p>I already have the book, chapter, and paragraph number.</p> <p>For example: </p> <pre><code>INSERT INTO `book` VALUES(1, 5, 16, 'This is a sentence from book 1, chapter 5, paragraph 16.'); </code></pre> <p>would convert to:</p> <pre><code>INSERT INTO `book` VALUES(1, 5, 16, 1,'This'); INSERT INTO `book` VALUES(1, 5, 16, 2,'is'); INSERT INTO `book` VALUES(1, 5, 16, 3,'a'); INSERT INTO `book` VALUES(1, 5, 16, 4,'sentence'); INSERT INTO `book` VALUES(1, 5, 16, 5,'from'); INSERT INTO `book` VALUES(1, 5, 16, 6,'book'); INSERT INTO `book` VALUES(1, 5, 16, 7,'1,'); INSERT INTO `book` VALUES(1, 5, 16, 8,'chapter'); INSERT INTO `book` VALUES(1, 5, 16, 9,'5,'); INSERT INTO `book` VALUES(1, 5, 16, 10,'paragraph'); INSERT INTO `book` VALUES(1, 5, 16, 11,'16.'); </code></pre> <p>Any assistance would be greatly appreciated ...Thanks!</p> <p>EDIT: Below is the code that worked ....based off of Akam's answer ...I changed the SQL statement. Added the [SEP] to separate the text from the other data which allowed me to work with the text. Also, added the $x to increment the word count.</p> <p>SQL:</p> <pre><code>1, 1, 1,[SEP]These are the words in sentence 1 in chapter 1 in a book. 1, 1, 2,[SEP]These are the words in sentence 2 in chapter 1 in a book. </code></pre> <p>PHP:</p> <pre><code>$lines = file('my-book.sql'); $query = "INSERT INTO `book` VALUES \n"; foreach ($lines as $line) { $part = preg_split("/\[SEP\]/", $line); $part[1] = preg_replace('~[\r\n]+~', '', $part[1]); $words = preg_split("/\s+/", $part[1]); $x = 1; foreach($words as $word) { $values[] = "(".$part[0]." ".$x.", '".$word."')"; $x++; } } $new_lines = implode(",\n", $values); $new_lines = $query.$new_lines.";"; echo $new_lines; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload