node-mysql

使用nodejs连接mysql数据库实现增删改查

余生长醉 提交于 2020-08-11 02:47:37
首先要有数据库 使用xampp 或者 phpstudy 可以傻瓜式安装 新建一个项目文件夹 之后在这个目录下初始化package.json (npm init) 先在项目中安装mysql 和 express ,这个项目里使用express 因为express实现路由比较方便 cnpm install mysql express --save 已经安装好mysql和express 接下来创建app.js 在app.js里引入express并实例化express对象 在app.js里引入mysql 开启一个服务器 接下来创建连接 使用db.connect()方法连接 ,这个方法接收一个参数 有错误就报错 创建数据库 在一个路由里写sql语句 使用db.query来执行sql语句 db.query()方法有两个参数 ,第一个参数是要执行的语句 第二个参数是个回调函数 回调函数里可以接收错误信息,也有执行后回来的信息 依然是错误优先 接下来在浏览器里访问127.0.0.1:3000/createdb 页面上显示创建成功 数据库里已经有nodemysql数据库了 这个时候就可以在配置连接数据库里加上当前的数据库了 创建表 也是在一个路由里写sql语句 类型是 int 数值 AUTO_INCREMENT 让id 自增, VARCHAR(255) 字符串 长度255,PRIMARY KEY(ID

What is the difference between single ( ? ) and double question mark ( ?? ) in node-mysql?

人盡茶涼 提交于 2020-05-08 06:47:10
问题 Am sure this is fairly obvious, to me it would make more sense if the second double question mark in the example was a single one. From their docs: Alternatively, you can use ?? characters as placeholders for identifiers you would like to have escaped like this: var userId = 1; var columns = ['username', 'email']; var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(err, results) { // ... }); console.log(query.sql); // SELECT `username`, `email`

What is the difference between single ( ? ) and double question mark ( ?? ) in node-mysql?

我与影子孤独终老i 提交于 2020-05-08 06:46:20
问题 Am sure this is fairly obvious, to me it would make more sense if the second double question mark in the example was a single one. From their docs: Alternatively, you can use ?? characters as placeholders for identifiers you would like to have escaped like this: var userId = 1; var columns = ['username', 'email']; var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(err, results) { // ... }); console.log(query.sql); // SELECT `username`, `email`

What is the difference between single ( ? ) and double question mark ( ?? ) in node-mysql?

纵饮孤独 提交于 2020-05-08 06:45:30
问题 Am sure this is fairly obvious, to me it would make more sense if the second double question mark in the example was a single one. From their docs: Alternatively, you can use ?? characters as placeholders for identifiers you would like to have escaped like this: var userId = 1; var columns = ['username', 'email']; var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(err, results) { // ... }); console.log(query.sql); // SELECT `username`, `email`

node-mysql multiple statements in one query, execute conditionally on response from each

Deadly 提交于 2020-03-05 02:07:11
问题 I want to execute multiple queries in one API call, but I only want to execute them conditionally based on the response of the previous one. How can I go about implementing this? I've seen that I can do something like below to execute multiple queries. connection.query('SELECT * ...; SELECT * ...', [1, 2], function(err, results) { if (err) throw err; }); But how do I await the response of the first before conditionally executing the second? 来源: https://stackoverflow.com/questions/60230245

How can I fetch the warnings after node-mysql queries

夙愿已清 提交于 2020-03-03 11:49:29
问题 How can I fetch the corresponding warning identified after the query execution as in: connection.query( squery, function(err, rows){ ... // search for OkPacket in 2 dimension array var warningCounter = okPacket.warningCount; if ( warningCounter > 0 ){ ??? } }); 回答1: Execute the query: SHOW WARNINGS Here is more on SHOW WARNINGS Syntax 来源: https://stackoverflow.com/questions/22962635/how-can-i-fetch-the-warnings-after-node-mysql-queries

How can I fetch the warnings after node-mysql queries

时光毁灭记忆、已成空白 提交于 2020-03-03 11:48:04
问题 How can I fetch the corresponding warning identified after the query execution as in: connection.query( squery, function(err, rows){ ... // search for OkPacket in 2 dimension array var warningCounter = okPacket.warningCount; if ( warningCounter > 0 ){ ??? } }); 回答1: Execute the query: SHOW WARNINGS Here is more on SHOW WARNINGS Syntax 来源: https://stackoverflow.com/questions/22962635/how-can-i-fetch-the-warnings-after-node-mysql-queries

MySQL与Node.js

萝らか妹 提交于 2020-02-27 20:55:13
我刚刚开始接触Node.js。 我来自PHP背景,因此我习惯于使用MySQL满足所有数据库需求。 如何在Node.js中使用MySQL? #1楼 通过安装库连接mysql数据库。 在这里,选择了稳定且易于使用的node-mysql模块。 npm install mysql@2.0.0-alpha2 var http = require('http'), mysql = require('mysql'); var sqlInfo = { host: 'localhost', user: 'root', password: 'urpass', database: 'dbname' } client = mysql.createConnection(sqlInfo); client.connect(); 对于NodeJS mysql连接和查询示例 #2楼 由于这是旧线程,因此只需添加更新: 要安装MySQL node.js驱动程序: 如果仅 npm install mysql ,则需要与运行服务器所在的目录相同。 我建议按照以下示例之一进行操作: 对于全局安装: npm install -g mysql 对于本地安装: 1-将其添加到依赖项的 package.json 中: "dependencies": { "mysql": "~2.3.2", ... 2-运行 npm install

Recommended way to change database in existing connecction with node-mysql

偶尔善良 提交于 2020-01-24 04:03:23
问题 I am trying to change database in an existing connection in node. The connection may or may not have database name in createConnection call. Here's the code: var mysql = require('mysql'); connection = mysql.createConnection( { host : 'localhost', user : 'me', password : 'secret', port : 3306, /* database : 'test' // optional */ }); I am using node-mysql lib. Currently I'm closing the connection and re-connecting. Is there any better way to do that? Similar to mysql_select_db in php? 回答1: You

Insert array of records into mysql with Node JS

随声附和 提交于 2020-01-23 06:58:05
问题 I have a array of data something like var records = [ {Name: '', Id: 1}, {Name: '', Id: 2}, {Name: '', Id: 3}, {Name: '', Id: 4}, {Name: '', Id: 5}, {Name: '', Id: 6} ]; there could be thousands of items inside records array... Ques1: Can we create a stored procedure which will accept an array of objects in mysql? Ques2: Is there a way to bulk insert this data into mysql with Node JS? 回答1: You can bulk insert the array of records ,but before that you might need to convert it into array of