Create bulk nodes - PDOException Problem

I'm trying to create multiple nodes at once using a CSV file for the data. However my script only creates the first 25 before it throws the following error. Any ideas what might be happening?


  PDOException: in drupal_write_record() (line 7166 of C:\XAMPP\htdocs\drupalie\includes\common.inc).


Set-Up

I have a custom form which creates the nodes on submit. The form just has a submit button.

This is the submit handler. I only have one column in my CSV sheet so I don't actually use the delimiter. First I generate an array with each node name.

  if (($handle = fopen("sites/all/modules/custom/generatenodes/nodes_database_import.csv", "r")) !== FALSE) {
    //open the CSV file
    $length = 1000;
    $delimiter = "|";
    //set the length and delimiter.
    while (!feof($handle)) {
      //$line is an array of the csv elements
      $node_name_array[] = fgetcsv($handle, $length, $delimiter);
    }

    fclose($handle);
  }


The array looks like this:

I then create the nodes:

global $user;
foreach ($node_name_array as $nodename) {
  if($nodename == FALSE) {
    return;
  }

    $values = array(
      'type' =&gt; 'school',
      'uid' =&gt; $user-&gt;uid,
      'status' =&gt; 1,
      'comment' =&gt; 0,
      'promote' =&gt; 0,
    );
    $entity = entity_create('node', $values);

    $ewrapper = entity_metadata_wrapper('node', $entity);

    $ewrapper-&gt;title-&gt;set($nodename['0']);
    $ewrapper-&gt;field_country-&gt;set($schoolname['0']);
     $ewrapper-&gt;save(true);
    entity_save('node', $entity);
}


But it breaks after the first 25 or so. What have I missed?

EDIT--

So I found the cause. It's to do with the values in my CSV file. Sometimes it likes them othertimes it doesn't. If I replace these values then it works up to the next broken value.

The first problem was a value with an accented e inside. Removing the accent fixed it.

Unfortunately the next two which broke it are not so obvious:


Putian Cambridge English 
Zhangzhou Bilingual Middle School


Yet the following values work:


Quanzhou Foreign Language Middle School
International College of Xiamen University


I've only pasted in clean values, but something is breaking it. Any ideas?

Trying to navigate to the content page after causes MySQL to break. Here's the error log:

https://www.dropbox.com/s/jji3cw559filq03/mysqlcrash_log.txt
You should be using migrate for this: https://www.drupal.org/project/migrate, it covers many of the pitfalls you are currently encountering when creating or importing content from a CSV, database or XML file. Don't recreate the wheel when someone has already created it for you. 

It's not like the feeds module or other "import" type modules, it's an entirely  code based OOP solution. You just extend the main migration class and map your data. It comes with a number of built in PHP functions to process the data further and it literally covers almost every scenario you could possibly encounter when mass creating or transferring content. 

As someone who's been down this path many times, it will save you a lot of time and hair pulling. I recommend looking at the code examples migrate provides.