【发布时间】:2015-08-19 22:23:23
【问题描述】:
我是 Electron 的新人。
我正在尝试使用 Electron 构建一个小型应用程序。我要做的是插入从前端获取的数据。
我正在使用 SQL 有数据库并且作为前端的反应可以提供任何建议。
如何插入数据? 谁能给我解决办法?
请帮帮我。
我试过这样:这是我的索引 .html,我需要发布从这里获取的数据。在 ZIpcode.js 中,MySQL 代码将在那里。
索引.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')
}
}
【问题讨论】:
-
与我们分享您已经尝试过的内容。
-
以上是我的代码,但无法正常工作,请您给我解决方案
-
你能说得更具体点吗?你有例外吗?数据是否从服务器成功传递到客户端?调试服务器/客户端代码时,哪一行导致错误?
-
我试图从客户端插入它,即从反应我试图将数据发布到数据库中,而不是从数据库中检索数据,在电子中我想知道我需要在哪里写邮政服务?
-
我使用 react.js 作为前端,它在上面的内容中,这里发生的是它正确存储数据,但我感到困惑的是,当我删除索引中的警报消息时。 js它没有进入连接。你能检查一下给我解决方案吗提前谢谢。