Inserting a Row in the Database

We can create a new row in just one simple SQL transaction called from a Node.js function


In your Cloud Backend interface :
- Click on API tab
- New function button


IFrame


Then you can write your function like this. Note that we receive the parameters: firstName, lastName, and email that we will insert in the 'users' table



Click on 'Try' button and check on your table :



 
📘  NoteThis function uses our NPM package: appdrag-cloudbackend on NPM


A common use case is creating a new row in your database then obtaining the latest id inserted. Here is how you can accomplish that in just one simple SQL transaction called from a Node.js function:


var cloudbackend = require('appdrag-cloudbackend'); 
cloudbackend.init(APIKey, appID); 
cloudbackend.sqlSelect("INSERT INTO Users (name, email, password) " 
+"values('john doe', 'john@doe.com', 'Test97_626p'); SELECT LAST_INSERT_ID() as newUserID;") 
.then( function(response) { 
        var result = JSON.parse(response); 
        var newUserID = result.Table[0].newUserID; 
        callback(null, newUserID); //return the newUserID 
});


This function uses our NPM package: appdrag-cloudbackend on NPM