zoukankan      html  css  js  c++  java
  • ch2-mysql相关

    1 mySql数据库基本
    1.1 创建表必须字段 id
    1.2 nodeJS数据库连接
    根目录下建立 mysql.js 文件代码
    var mysql = require('mysql');

    var config = mysql.createConnection({
    host: 'localhost', //数据库的地址
    user: 'root', //数据库的用户名
    password: '', //数据库密码
    port: '3306', //端口号(数据库默认端口号)
    database: 'node' //使用哪个数据库
    });
    // 开始连接数据库
    config.connect();
    //操作数据库
    config.query('SELECT * FROM test', function(err, data){
    console.log(data);
    });
    //结束 断开连接
    config.end();

    $ node mysql.js 测试数据库是否连接上

    2 mySQL数据库 数据库的基本操作
    常用类型 int(11) longText varChart(64)
    2.1 创建 表 node这个数据库里面创建一个user表
    CREATE TABLE `node`.`user`(
    id 字段 int(11)数据类型最大的值是11位 AUTO_INCREMENT重要--id自动增加
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(64) NOT NULL ,
    `pass` varchar(64) NOT NULL ,
    PRIMARY KEY (`id`) // 重要: 主键
    )
    ENGINE = InnoDB;

    CREATE TABLE `node`.`user`(
    `id` INT NOT NULL AUTO_INCREMENT,
    `username` varchar(64) NOT NULL ,
    `pass` varchar(64) NOT NULL ,
    PRIMARY KEY (`id`)
    )
    ENGINE = InnoDB CHARSET=utf8;

    2.2 增
    固定代码 哪个表 key
    INSERT INTO `wenzhagn` (`id`, `username`, `pass`) VALUES ('1', 'wulv', '123');

    添加字段
    alter table `article` add class INT(11) NOT NULL;
    alter table `article_type` add typeunder INT(11) NOT NULL;


    2.3 删
    固定代码 删article表 因为id是唯一的 id = 0
    DELETE FROM `article` WHERE `id` = 0

    2.3 改
    哪个表 id为15的这一条数据里 content的内容
    update `xxxx` set `username`='112313' where `id`=6666;

    更新多个数据
    update `user` set `username`=? , `admin`=? where `id`=6
    update `banner` set `url`='/img/1490544843424.jpg' where `name`=04 and `type`= 4

    2.4 查询shuoshuo这个表里的所有内容
    SELECT * FROM `shuoshuo` WHERE id = ?
    或 SELECT * FROM `shuoshuo` WHERE username = ?
    SELECT * FROM article

    限制查询 查询开始到结束 第0条开始查3条数据
    SELECT * FROM article limit 0,3

    排序查询 按照id排序 降序 新到旧
    SELECT * FROM article order by id desc limit 3 //按id降序显示3条记录
    SELECT * FROM article order by id desc limit 1,3 //从序列号1开始显示3条记录

    3 导出mysql操作的模块
    3.1 将mysql移动到 module/ 自定义模块目录
    module/mysql.js

    3.2 将mysql的操作作为模块暴露出去
    var mysql = require('mysql'); //使用mysql模块操作mysql数据库

    module.exports = function(sql, val, callback){ //导出这个模块
    //参数1 mysql数据的操作(创增删改等mysql数据库的操作)
    //参数2 页面传递的值
    //回调函数
    //创建mysql连接信息
    var config = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    port: '3306',
    database: 'node'
    });

    //开始连接数据库
    config.connect();

    //操作数据库

    // 静态操作方式
    /*config.query('SELECT * FROM user' , function(err, data){ //查询node数据库的test表
    console.log(data);
    });*/

    //动态操作方式
    config.query(sql, val, callback);

    //断开数据库连接
    config.end();

    };
  • 相关阅读:
    mysql分表分库 ,读写分离
    二级域名解析设置及Apache 子域名配置
    PHP错误检测
    PHP缓存技术相关
    高并发一些处理办法
    memcached 安装与简单实用使用
    数据库入门
    函数
    结构体 枚举
    c# 6大类集合
  • 原文地址:https://www.cnblogs.com/easyweb/p/6641696.html
Copyright © 2011-2022 走看看