Way to use Cordova-plugin-sqlServer : problem

Hi,
I’m new on ionic and I’ve some problem with cordova non-native plugin. After few long hours of search and reading I don’t find out how to use non-native plugin…

When I use

import { SqlServer } from 'cordova-plugin-sqlserver' 

It’s doesn’t works .

When I uses :

declare var SqlServer : any;

[...]

window.plugins.SqlServer.init(...);

and the other ways likes (window).SqlServer don’t works also…

The error is : Cannot read property ‘SqlServer’ of undefined

Can you help me to solve this, or to use a non-native plugin ? With all this days on this I’m limit to think that is the plugin who doesn’t works and not my ways to uses it.

thanks for the futur answers :slight_smile:

Also I’m French so, sorry for the languages errors :confused:

I work with sublimText, and I emulate on my browser (google Chrome)

My ionic info : Capture

Thanks

Are you doing your testing from a web browser???

> ionic serve

… or a device?

> ionic cordova emulate/run android

I use

ionic cordova emulate android -lc --adress:localhost

Obviously it will not work on the browser as this is a cordova plugin for iOS and Android only.

You don’t need an import at all as it doesn’t have an Ionic Native version (unless it does and I just haven’t found it)

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

declare var SqlServer: any;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {
    this.doSomething();
  }

  doSomething() {
    SqlServer.init('192.168.123.123', 'MSSQLSERVER', 'sa', 'password', result => {
      console.log(JSON.stringify(result));
    })
  }

}

1 Like

Ok, I’m gonna try this, thanks for the answer :slight_smile:

Hi @Judgewest2000

It works ! Thanks a lot ! (I had some problem due of the test on real devices but now I can tell you if is working or not :slight_smile: )

Have a good day :slight_smile:

Dear @Judgewest2000
When I use this code on iphone, it get error…

warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.

(lldb)

Can you help me ?Please

By Ivan

I’m afraid I know nothing about the plugin.

My answer was based on using a plugin within typescript.

Additionally I would NEVER suggest using it, you should put your dB behind a firewall and only access it via API calls.

@Judgewest2000

Thanks a lot ^^

By Ivan

I find the way now

Dear @Judgewest2000 , your code missing something

[SqlServer.init(‘192.168.123.123’, ‘MSSQLSERVER’, ‘sa’, ‘password’, ] is missing the database name

the correct string is : “192.168.0.120”, “SQLEXPRESS”, “sa”, “01234567”, “dinademo”

Ivan

As stated before, I’m not an expert on this plugin

Yes, I know that,
And now just want to answer the question, and tell you the way can make it, if someone read this post , can find the answer.

Thanks a lot

Ivan

If the app is going to be distributed at all, using this plugin potentially constitutes a huge security risk, as @Judgewest2000 suggested above. Anybody with a copy of the app binary has the ability to execute arbitrary SQL directly against your server.

IMHO, it is always better, even if seeming tedious in the beginning, to write a middleware application server that receives REST requests over HTTP and handles all interaction with the database. If one wants to develop and test a frontend app without having that infrastructure in the backend, instead of resorting to direct SQL access as is described in this thread, I would instead isolate all upstream server interaction into a service (I always call mine MothershipService) and mock out that service with a class that contains dummy data.

1 Like

Dear @rapropos

Thank you very much,

Can u tell me more about :
" I would instead isolate all upstream server interaction into a service "

Thanks again,
By Ivan

I would suggest following the patterns in part 4 of the Tour of Heroes.