Skip to main content
Inspiring
February 13, 2019
Answered

Script to import mysql data and use it as variable data

Is there any script to import data from mysql and use it as variable data?

Currently the option I'm looking at is getting data from mysql and using a json to csv converter and then using the csv in variable data.

However it seems like this is quite tiresome and can cause some errors if there's any invalid characters or problems int he json to csv.

I'm running cc2019 illustrator.  Any ideas how this can be accomplished?

This topic is closed to new replies. Start a new post to keep the conversation going.
Ten A
Ten ACorrect answer
Community Expert
February 19, 2019

Illustrator doesn't have any abilities to connect databases. However, We can use CEP infrastructure with NPM.

Here is a sample of V8 side script that connects MySQL and throws command to get return values.

var csi = new CSInterface();

$("#get").click(function() {

  var mysql = require('mysql');

  var connection = mysql.createConnection({

    host : '<MySQL Server address>',

    port : '<Port: probably 3306>',

    user : '<database user>',

    password : '<user password>',

    database : '<database name>'

    });

  connection.connect();

  var queryString = "SELECT `order_id` AS `order_id`, `size` AS `size`, `quantity_order` AS `quantity_order`, "

  + "`post` AS `post`, `adds` AS `adds`, `name` AS `name`, `delivered` AS `delivered` "

  + "FROM `book_db`.`work_data` AS `work_data` WHERE `delivered` = 0 ORDER BY `size` ASC";

  connection.query(queryString, function(err, rows, fields) {

  if (err) console.log(err);

  else {

  var str = "";

  for (var i=0;i<rows.length;i++){

  str += rows.order_id + "," + rows.size + "," +  rows.quantity_order + ","

         +  rows.post + "," +  rows.adds + "," +  rows.name +"\r"

  }

  csi.evalScript("formatter('" + str + "')");

  }

  });

Also, you can check about CEP Extensions in below forum.

Extensions / Add-ons Development

3 replies

Ten A
Community Expert
Ten ACommunity ExpertCorrect answer
Community Expert
February 19, 2019

Illustrator doesn't have any abilities to connect databases. However, We can use CEP infrastructure with NPM.

Here is a sample of V8 side script that connects MySQL and throws command to get return values.

var csi = new CSInterface();

$("#get").click(function() {

  var mysql = require('mysql');

  var connection = mysql.createConnection({

    host : '<MySQL Server address>',

    port : '<Port: probably 3306>',

    user : '<database user>',

    password : '<user password>',

    database : '<database name>'

    });

  connection.connect();

  var queryString = "SELECT `order_id` AS `order_id`, `size` AS `size`, `quantity_order` AS `quantity_order`, "

  + "`post` AS `post`, `adds` AS `adds`, `name` AS `name`, `delivered` AS `delivered` "

  + "FROM `book_db`.`work_data` AS `work_data` WHERE `delivered` = 0 ORDER BY `size` ASC";

  connection.query(queryString, function(err, rows, fields) {

  if (err) console.log(err);

  else {

  var str = "";

  for (var i=0;i<rows.length;i++){

  str += rows.order_id + "," + rows.size + "," +  rows.quantity_order + ","

         +  rows.post + "," +  rows.adds + "," +  rows.name +"\r"

  }

  csi.evalScript("formatter('" + str + "')");

  }

  });

Also, you can check about CEP Extensions in below forum.

Extensions / Add-ons Development

Translate
CarlosCanto
Community Expert
Community Expert
February 13, 2019

yeah, Illustrator does not support mySql format as is, you'll have to export your data to csv preferably.

Translate
Silly-V
Legend
February 13, 2019

Just use the 'save as csv' feature of your favorite db management software and then you can either use the VariableImporter.jsx script or the native csv-import feature of Illustrator versions CC2018+.

Translate