How to connect phonegap / Apache Cordova to SQL Server?

2

I have just started in the world of hybrid applications, with Apache Cordova the detail is that it is only HTML, CSS and JS; the problem is that I do not know how to connect direct JavaScript with SQL.

Does anyone know how to do it, or any alternative?

    
asked by Erick Tovar 28.12.2016 в 05:16
source

4 answers

2

I developed a plugin to connect directly to SQL Server without going through an intermediate server, it is open source and can be installed like any other plugin. Here you can see the project page on GitHub (or npm ).

The plugin connects right to the database, as if it were a desktop app. That is, you put the parameters of the database, server, user, password, etc. and then you run querys directly.

This is an example of how it works. First connecting to the database and then making a query:

// inicializar conexión a la base de datos
SqlServer.init("192.168.0.120", "SQLEXPRESS", "sa", "pass", "db", function(event) {
  alert(JSON.stringify(event));
}, function(error) {
  alert(JSON.stringify(error));
});

// ejecución de una consulta
SqlServer.executeQuery("select * from test_table where test_code=1", function(event) {
  alert(JSON.stringify(event));
}, function(error) {
  alert("Error : " + JSON.stringify(error));
});
    
answered by 30.09.2017 в 07:24
1

Another option would be to create a webservice that gives you a json to get the necessary information.

    
answered by 28.12.2016 в 12:59
0

If you mean to instantiate it within an application, you can not. The most common way to do persistence in hybrid applications with cordova is to install the plugin sqlite de cordova (and in native create directly a database sqlite) and connect to any service hosted on your server that is connected to the database engine you want. Thus, you receive from your server the necessary data, you make the persistence in your local sqlite database of your application and send and receive data from / to your server.

    
answered by 28.12.2016 в 12:16
0

If you have a database in production in SQLServer, then you have no choice but to use it in effect. If it is a new database, I recommend you think well if you really need SQLServer.

  • Do you need to keep records intensively?
  • Is it possible that there are many writing operations at the same moment?

If the answer is yes to the questions then SQLServer is your best option. In applications where the writing is very intensive and where there can be multiple transactions in parallel, SQLite is not a good option because this engine only supports one transaction at the moment.

  

SQLite has a "locking" mechanism which locks the database in a writing application to ensure that there is only one transaction at a given moment

SQLite is a de facto alternative for mobile applications, it is fast, it is ACID compilant and above all, portable. 90% of the mobile applications you have installed, use this database engine.

Using an API to communicate remote database

It is not possible (at least, as far as I understand) to use SQLS in Cordova, I have not found plugin for this. However, even if there were, I think it would not be the optimal way to have a successful communication. A REST API is very useful in these scenarios, since the business logic is abstracted into a single service that can be shared among several clients.

For example, having an API, isolate all the logic of your system and from your Cordova application, web or even desktop, you could consume it and carry out the same operations, ignoring completely how they are implemented and what Database engine is used.

For example, the following actions can be consumed from any client:

  • GET /users : get all users
  • GET /users/:id : get a user by id
  • POST /users : create a user
  • PUT /users/:id : update a user
  • DELETE /users/:id : delete a user

From your Cordova application you only have to use AJAX to communicate with the API:

$.ajax({
  url: '/users',
  type: 'GET'
  dataType: 'json'
})
.done(users => {
  // hacer algo con los usuarios
});

NOTE: If you use this method remember to set the origin : <access origin="*" /> .

    
answered by 28.12.2016 в 15:17