Luke Kuhner
Luke Kuhner

Reputation: 41

Connect Cordova hybrid app to Azure SQL Database

I am using apache cordova and ionic framework in Visual studio 2015 to create a hybrid app.

I would like to connect my app to a cloud sql database such as azure sql or google cloud sql.

I need to be able to do inserts, updates, and selects.

From what I have researched I understand that I may have to use PHP, but I haven't found any good examples to go by. Any help is much appreciated.

Upvotes: 4

Views: 1088

Answers (2)

meet-bhagdev
meet-bhagdev

Reputation: 2708

kuhner, Cordova is a Node.js framework. You can certainly use Azure SQL Database with Node.js. There are quite a few great driver options that can help you get started on the platform of your choice:
- node-mssql
- tedious

Here is a quick code sample:

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'yourusername',  
    password: 'yourpassword',  
    server: 'yourserver.database.windows.net',  
    // When you connect to Azure SQL Database, you need these next options.  
    options: {encrypt: true, database: 'AdventureWorks'}  
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
    // If no error, then good to proceed.  
    console.log("Connected");  
    executeStatement();  
});  

var Request = require('tedious').Request;  
var TYPES = require('tedious').TYPES;  

function executeStatement() {  
    request = new Request("SELECT c.CustomerID, c.CompanyName,COUNT(soh.SalesOrderID) AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC;", function(err) {  
    if (err) {  
        console.log(err);}  
    });  
    var result = "";  
    request.on('row', function(columns) {  
        columns.forEach(function(column) {  
          if (column.value === null) {  
            console.log('NULL');  
          } else {  
            result+= column.value + " ";  
          }  
        });  
        console.log(result);  
        result ="";  
    });  

    request.on('done', function(rowCount, more) {  
    console.log(rowCount + ' rows returned');  
    });  
    connection.execSql(request);  
}  

To get started with Azure SQL DB, please follow the link: https://azure.microsoft.com/en-us/documentation/articles/sql-database-get-started/

Upvotes: 1

user1650034
user1650034

Reputation: 11

MSDN Proof of concept link https://msdn.microsoft.com/library/mt715784.aspx

Step 1: Connect The new Connection function is used to connect to SQL Database. JavaScript

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'yourusername',  
    password: 'yourpassword',  
    server: 'yourserver.database.windows.net',  
    // If you are on Microsoft Azure, you need this:  
    options: {encrypt: true, database: 'AdventureWorks'}  
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
// If no error, then good to proceed.  
    console.log("Connected");  
});  

Step 2: Execute a query All SQL statements are executed using the new Request() function. If the statement returns rows, such as a select statement, you can retreive them using the request.on() function. If there are no rows, request.on() function returns empty lists. JavaScript

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'yourusername',  
    password: 'yourpassword',  
    server: 'yourserver.database.windows.net',  
    // When you connect to Azure SQL Database, you need these next options.  
    options: {encrypt: true, database: 'AdventureWorks'}  
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
    // If no error, then good to proceed.  
    console.log("Connected");  
    executeStatement();  
});  

var Request = require('tedious').Request;  
var TYPES = require('tedious').TYPES;  

function executeStatement() {  
    request = new Request("SELECT c.CustomerID, c.CompanyName,COUNT(soh.SalesOrderID) AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC;", function(err) {  
    if (err) {  
        console.log(err);}  
    });  
    var result = "";  
    request.on('row', function(columns) {  
        columns.forEach(function(column) {  
          if (column.value === null) {  
            console.log('NULL');  
          } else {  
            result+= column.value + " ";  
          }  
        });  
        console.log(result);  
        result ="";  
    });  

    request.on('done', function(rowCount, more) {  
    console.log(rowCount + ' rows returned');  
    });  
    connection.execSql(request);  
}  

Step 3: Insert a row In this example you will see how to execute an INSERT statement safely, pass parameters which protect your application from SQL injection vulnerability, and retrieve the auto-generated Primary Key value. JavaScript

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'yourusername',  
    password: 'yourpassword',  
    server: 'yourserver.database.windows.net',  
    // If you are on Azure SQL Database, you need these next options.  
    options: {encrypt: true, database: 'AdventureWorks'}  
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
    // If no error, then good to proceed.  
    console.log("Connected");  
    executeStatement1();  
});  

var Request = require('tedious').Request  
var TYPES = require('tedious').TYPES;  

function executeStatement1() {  
    request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP);", function(err) {  
     if (err) {  
        console.log(err);}  
    });  
    request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');  
    request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');  
    request.addParameter('Cost', TYPES.Int, 11);  
    request.addParameter('Price', TYPES.Int,11);  
    request.on('row', function(columns) {  
        columns.forEach(function(column) {  
          if (column.value === null) {  
            console.log('NULL');  
          } else {  
            console.log("Product id of inserted item is " + column.value);  
          }  
        });  
    });       
    connection.execSql(request);  
}

Upvotes: 1

Related Questions