How to insert data into database in electron?

how to insert data into database in electron SQl as database And React as front end

  • I am new at Electron. I am trying to build a small app using Electron. What I am trying to do is to insert the data which is taken from front end. I am using SQL has database and react as front end can any give suggestions. How to insert data? Can any one please give me the solution for it? Please help me. I tried like this: this is my Index .html and I need to post the data which is taken from here. And in ZIpcode.js MySQL code will be there. Index.html <div> <form className =" col-lg-12 form-horizontal" role="form" > <nav className="navbar navbar-inverse navbar-fixed-top"> <div className = "container-fluid"> <div className="navbar-header"> <div className="col-lg-12 col-sm-12 col-xs-12"> <div className ="row"> <div className="col-lg-2 col-sm-2 col-xs-2"> <button type="submit" id="btnSubmit" className="btn btn-primary center-block"> Submit </button> </div> <div className="col-lg-1 col-sm-1 col-xs-1"> <button type="reset" className="btn btn-danger center-block"> Cancel </button> </div> </div> </div> </div> </div> </nav> <br/><br/><br/> <div className ="container"> <div className = "row"> <div className = "row"> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <div className="col-lg-1 col-sm-1"> </div> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="ZipCode">Zip Code:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="number" className="form-control" id="ZipCode" ref = "ZipCode" placeholder="Zip Code" required="true"/> </div> <div className="col-lg-1 col-sm-1"> </div> </div> </div> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="ShortCode">Short Code:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="Short Code" className="form-control" id="ShortCode" ref = "ShortCode" placeholder="Short Code" required="true"/> </div> <div className="col-lg-2 col-sm-2"> </div> </div> </div> </div> <div className = "row"> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <div className="col-lg-1 col-sm-1"> </div> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="City">City:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="City" className="form-control" id="City" ref = "City" placeholder="City" required="true"/> </div> <div className="col-lg-1 col-sm-1"> </div> </div> </div> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="State">State:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="State" className="form-control" id="State" ref = "State" placeholder="State" required="true"/> </div> <div className="col-lg-2 col-sm-2"> </div> </div> </div> </div> <div className = "row"> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <div className="col-lg-1 col-sm-1"> </div> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="PhoneAreaCode">Phone Area Code:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="PhoneAreaCode" className="form-control" id="PhoneAreaCode" ref = "PhoneAreaCode" placeholder="Phone Area Code" required="true"/> </div> <div className="col-lg-1 col-sm-1"> </div> </div> </div> <div className="col-lg-6 col-sm-6 col-xs-6"> </div> </div> <div className = "row"> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <div className="col-lg-1 col-sm-1"> </div> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="TaxCode">Tax Code:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="TaxCode" className="form-control" id="TaxCode" ref = "TaxCode" placeholder="Tax Code" required="true"/> </div> <div className="col-lg-1 col-sm-1"> </div> </div> </div> <div className="col-lg-6 col-sm-6 col-xs-6"> <div className="form-group"> <label className="control-label col-lg-4 col-sm-4 col-xs-12" for="Tax">%Tax:</label> <div className="col-lg-6 col-sm-6 col-xs-12"> <input type="number" className="form-control" id="Tax" ref = "Tax" placeholder="Tax" required="true"/> </div> <div className="col-lg-2 col-sm-2"> </div> </div> </div> </div> </div> </div> </form> </div> zipcode.js window.onload = function() { document.getElementById("btnSubmit").onclick = function () { alert('Submit button click'); var v1 = document.getElementById('ZipCode').value; var v2 = document.getElementById('ShortCode').value; var v3 = document.getElementById('City').value; var v4 = document.getElementById('State').value; var v5 = document.getElementById('PhoneAreaCode').value; var v6 = document.getElementById('TaxCode').value; var v7 = document.getElementById('Tax').value; //alert("ZipCode:"+v1+" "+"ShortCode:"+v2+" "+"City:"+v3) //sql connection //alert('after Connection'); var Connection = require('tedious').Connection; var config = { userName: 'xx', password: 'xxxx', server: 'xxxxxx', // You can use 'localhost\\instance' to connect to named instance database: 'XXXXX', options: { database: 'XXXXX' }, } alert("SQL") var connection = new Connection(config); connection.on('connect',function(err){ alert('hai') if(err){ console.log(err) } executeStatement(); }); Request = require('tedious').Request; function executeStatement(){ //alert(v1,v2,v3,v4,v5,v6,v7) var request = new Request("INSERT INTO XXXXX VALUES ("+v1+",'"+v2+"','"+v3+"','"+v4+"','"+v5+"','"+v6+"',"+v7+")", function(err,result){ console.log(request) if(err){ console.log(err); }else{ console.log(result) } }); connection.execSql(request); } alert('completion') } }

  • Answer:

    Tedious is a package that connects using the TDS protocol and AFIAK it does not support MySQL. If you are using MySQL, perhaps you should try http://docs.sequelizejs.com/en/latest/ or https://github.com/felixge/node-mysql/. Update: Based on your comment, it sounds like you need to change the order in which things execute. You can do this by moving the dependent code (the code to execute after connection) to the end of your connection callback function like this... var connection = new Connection(config); connection.on('connect',function(err){ alert('hai') if(err){ console.log(err) } else{ Request = require('tedious').Request; function executeStatement(){ //alert(v1,v2,v3,v4,v5,v6,v7) var request = new Request("INSERT INTO XXXXX VALUES ("+v1+",'"+v2+"','"+v3+"','"+v4+"','"+v5+"','"+v6+"',"+v7+")", function(err,result){ console.log(request) if(err){ console.log(err); }else{ console.log(result) } }); connection.execSql(request); } } alert('completion') }); Also, you may have missed a semicolon, and have an extra comma... var config = { userName: 'xx', password: 'xxxx', server: 'xxxxxx', // You can use 'localhost\\instance' to connect to named instance database: 'XXXXX', options: { database: 'XXXXX' } // <-- no comma here }; // <-- add semicolon here

Hussian Shaik at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.