文档结构:
|-public/
|-js/
|-shop.js
|-views/
|-routes/
|app.js
我已经在我的app.js中定义了我的SQL连接
const mysql = require('mysql');
const db = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'password',
database: 'pfis'
});
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
app.js下的所有sql查询都可以正常工作!
我的问题是我有一个shop.js文件(请参阅目录结构),一旦他们单击我页面上的按钮元素,就需要插入一些sql(存储过程)。而且我不知道如何实现这一目标。
shop.js示例(不起作用!):
function purchaseClicked() {
var stoel = prompt("Enter your chairnumber: ");
alert('Someone is on this way with the ATM-machine');
var cartItems = document.getElementsByClassName('cart-items')[0];
while (cartItems.hasChildNodes()) {
var itemTitle = document.getElementsByClassName('cart-item-title')[0].innerHTML;
var itemQuantity = document.getElementsByClassName('cart-quantity-input')[0].value;
db.query("Call test1_insert(" + itemTitle + ", " + itemQuantity + ", " + stoel + ");",
function (error, results, fields) {
if (error) {
alert("Something went wrong, try again!");
}
alert("Looks like all good!");
});
cartItems.removeChild(cartItems.firstChild);
}
updateCartTotal();
}
我试图在shop.js文件中从app.js添加相同的数据库连接代码(请参见上面的代码段),但它也不喜欢。
谁可以帮助我如何从“外部” .js文件执行SQL?
答案 0 :(得分:2)
我为此使用Sequelize。 像这样的db文件:
Expanded(
child: Container(
child: Padding(
padding: const EdgeInsets.only(
left: 8.0,
right: 8.0,
top: 8.0,
bottom: 4.0),
child: new TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
filled: true,
hintStyle: new TextStyle(
color: Colors.grey[800]),
hintText: "Supervisor",
fillColor: Colors.white70),
),
),
),
),
创建数据库文件后,您可以像这样到达商店:
var sequelize = new Sequelize(mysqlDatabase, mysqlUser,mysqlPassword, {
host: mysqlHost,
dialect: 'mysql',
pool: {
max: 1000,
min: 0,
idle: 10000
},
//logging: logger.info
logging: false
});
var db = {};
db.Shop = sequelize.import(__dirname + '/models/Shop.js');
module.exports = db;