Forum Discussion

S_Seydel's avatar
S_Seydel
Contributor
10 years ago

SQL insert with JScript arrays and adodb

Hello,   I have a problem with the following function:   function sqlInsert() { // static name of the database table var table = "Names"; // calls the function which opens the database con...
  • TanyaYatskovska's avatar
    TanyaYatskovska
    10 years ago

    Hi,

    You need to pass either strings or the safeArray arrays. You code doesn’t work as you pass the JScript array the the AddNew method. You can use two approaches:

    1. Update your table in the following way:
    recordset.AddNew();
    recordset("forename") = "John";
    recordset("surname") = "Doe";
    recordset.Update;

     

    1. Convert a JScript array to safeArray in the following way:
    function ArrayToSafeArray(array) {
        var dictionary = new ActiveXObject("Scripting.Dictionary");
        for (var i = 0; i < array.length; i++) {
          dictionary.add(i, array[i]);
        }
        return dictionary.Items();
      }
    
      var columns = new Array("forename", "surname");
      var values = new Array("John", "Doe");
      recordset.AddNew(ArrayToSafeArray(columns), ArrayToSafeArray(values));
      recordset.Update();