Inserting data into a table fieldtype via plugin?
The thing to keep in mind here is that a table field contains a simple JSON string. So you can (and should) use PHP's native json_encode function to prepare your data...

// Your data will be an array of arrays:
$tableData = array(
    array(
        "col1" =&gt; "data",
        "col2" =&gt; "data",
    )
);

// Then simply encode it when you set your field value:
$entry-&gt;getContent()-&gt;setAttributes(array(
    'tableFieldHandle' =&gt; json_encode($tableData)
));


This will also solve any string escaping concerns for you!