亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何nodejs鏈接mysql,增刪改查

如何nodejs鏈接mysql,增刪改查

慕標琳琳 2019-03-20 13:13:17
如何nodejs鏈接mysql,增刪改查
查看完整描述

4 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

  

接著使用nodejs對數據庫進行增刪改查:

  

  

//引入mysql模塊

var mysql = require('mysql');

//鏈接數據庫

var connection = mysql.createConnection({

  host:'localhost',

  user:'root',

  password:'root',

  database:'node',

})

connection.connect();

//查詢數據

var sql = 'SELECT * FROM user';

connection.query(sql,function (err,result) {

  if(err){

    console.log('error');

    return;

  }

  console.log('-----------------查詢----------------');

  console.log(result);

  console.log('-----------------查詢結束----------------');

})

  

//增加數據

var addsql = 'INSERT INTO user(name,age,sex) VALUES(?,?,?)';

var addsqlparams = ['汪丹萍','24','女'];

connection.query(addsql,addsqlparams,function (err,result) {

  if(err){

    console.log('error');

    return;

  }

  console.log('-----------------新增成功----------------');

  console.log(result);

  console.log('-----------------結束----------------');

})

  

//修改數據

var modsql = 'UPDATE user SET name = ?,age = ? WHERE id = ?';

var modsqlparams = ['呂雪源love','26','1'];

connection.query(modsql,modsqlparams,function (err,result) {

  if(err){

    console.log('err');

    return;

  }

  console.log('--------------------------------');

  console.log(result);

  console.log('--------------------------------');

})

  

//刪除數據

var delsql = 'DELETE FROM user where id = 2';

connection.query(delsql,function (err,result) {

  if(err){

    console.log('err');

    return;

  }

  console.log('----------刪除-------------');

  console.log(result);

})

  

  

connection.end();



查看完整回答
反對 回復 2019-03-29
  • 4 回答
  • 0 關注
  • 983 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號