文章目录
Node.js连接MySQL 查询数据
从表中查询数据
要从 MySQL 中的表中查询数据,请使用“SELECT”语句。
示例获取您自己的 Node.js 服务器
从“customers”表中查询所有记录,并显示结果对象:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
SELECT * 将返回所有列
将上面的代码保存在名为“demo_db_select.js”的文件中并运行该文件:
运行“demo_db_select.js”
C:\Users\Your Name>node demo_db_select.js
这将为您提供以下结果:
[
{ id: 1, name: 'John', address: 'Highway 71'},
{ id: 2, name: 'Peter', address: 'Lowstreet 4'},
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 4, name: 'Hannah', address: 'Mountain 21'},
{ id: 5, name: 'Michael', address: 'Valley 345'},
{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
{ id: 7, name: 'Betty', address: 'Green Grass 1'},
{ id: 8, name: 'Richard', address: 'Sky st 331'},
{ id: 9, name: 'Susan', address: 'One way 98'},
{ id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
{ id: 11, name: 'Ben', address: 'Park Lane 38'},
{ id: 12, name: 'William', address: 'Central st 954'},
{ id: 13, name: 'Chuck', address: 'Main Road 989'},
{ id: 14, name: 'Viola', address: 'Sideway 1633'}
]
选择列
要仅查询表中的某些列,请使用“SELECT”语句,后跟列名。
示例
从“customers”表中选择名称和地址,并显示返回对象:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT name, address FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
将上面的代码保存在名为“demo_db_select2.js”的文件中并运行该文件:
运行“demo_db_select2.js”
C:\Users\Your Name>node demo_db_select2.js
这将为您提供以下结果:
[
{ name: 'John', address: 'Highway 71'},
{ name: 'Peter', address: 'Lowstreet 4'},
{ name: 'Amy', address: 'Apple st 652'},
{ name: 'Hannah', address: 'Mountain 21'},
{ name: 'Michael', address: 'Valley 345'},
{ name: 'Sandy', address: 'Ocean blvd 2'},
{ name: 'Betty', address: 'Green Grass 1'},
{ name: 'Richard', address: 'Sky st 331'},
{ name: 'Susan', address: 'One way 98'},
{ name: 'Vicky', address: 'Yellow Garden 2'},
{ name: 'Ben', address: 'Park Lane 38'},
{ name: 'William', address: 'Central st 954'},
{ name: 'Chuck', address: 'Main Road 989'},
{ name: 'Viola', address: 'Sideway 1633'}
]
结果对象
从上面示例的结果中可以看出,结果对象是一个包含每一行作为对象的数组。
要返回例如第三条记录的地址,只需引用第三个数组对象的 address 属性:
示例
返回第三条记录的地址:
console.log(result[2].address);
将产生以下结果:
Apple st 652
Fields 对象
回调函数的第三个参数是一个数组,其中包含有关结果中每个字段的信息。
示例
从“customers”表中查询所有记录,并显示 fields 对象:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT name, address FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(fields);
});
});
将上面的代码保存在名为“demo_db_select_fields.js”的文件中并运行该文件:
运行“demo_db_select_fields.js”
C:\Users\Your Name>node demo_db_select_fields.js
这将为您提供以下结果:
[
{
catalog: 'def',
db: 'mydb',
table: 'customers',
orgTable: 'customers',
name: 'name',
orgName: 'name',
charsetNr: 33,
length: 765,
type: 253,
flags: 0,
decimals: 0,
default: undefined,
zeroFill: false,
protocol41: true
},
{
catalog: 'def',
db: 'mydb',
table: 'customers',
orgTable: 'customers',
name: 'address',
orgName: 'address',
charsetNr: 33,
length: 765,
type: 253,
flags: 0,
decimals: 0,
default: undefined,
zeroFill: false,
protocol41: true
}
]
从上面示例的结果可以看出,fields 对象是一个数组,其中包含有关每个字段的信息作为对象。
要返回第二个字段的名称,只需引用第二个数组项的 name 属性:
示例
返回第二个字段的名称:
console.log(fields[1].name);
这将产生以下结果:
address
总结
本文介绍了Node.js连接MySQL 查询数据,如有问题欢迎私信和评论
标签:Node,name,err,必知,MySQL,db,js,address,id From: https://blog.csdn.net/qq_24018193/article/details/145072673