1.MYSQL 语言的分类

(1) DDL 数据库定义

(2) DQL 数据库查询

(3) DML 数据库操作

(4) DCL  数据库权限

2.MYSQL  操作

(1) 创建数据库mysql> create database cmdb default charset utf8;

(2)查看所有的数据库mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| cmdb               |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

5 rows in set (0.00 sec)

(3) 使用cmdb数据库mysql> use cmdb;

(4) 查看数据库的创建语法mysql> show create database cmdb;

(5) 删除数据库mysql> drop database cmdb;

(6) 查看所有的表mysql> show tables;

(7)  创建用户表mysql> create table user(id int,name varchar(64),age int, sex boolean,telphone varchar(32), addr varchar(512))engine=innodb default charset utf8;

(8)  查看创建表的过程mysql> show create table user;

(9)  删除表mysql> drop table user;

(10)  查看表结构mysql> desc user;

(11)  插入数据mysql> insert into user(id,name,age,sex,telphone,addr)values(1,'李宽',25,1,'18829787559','陕西省西安市');

(12)  查看数据mysql> select * from user;

(13)  只查询指定的列mysql> select name,addr from user;

(14)  条件查询

where

逻辑关联词  and  or

关系表达式  >  =  <=  !=

like表达式

(1) % 占多位    'abc%'   '%abc'

(2) _ 占一位      ‘abc_’ '_abc'

in 的使用       colname  in (a,b)

not in 的使用   colname not in (a,b)select name,age,addr from user where addr = '陕西省西安市' and age=25;

mysql> select name,age,addr from user where addr = '陕西省西安市' or age = 25;

select name,age,addr from user where addr = '陕西省西安市' or age > 25;

mysql> select name,age,addr from user where age >= 25;

mysql> select name,age,addr from user where age != 25;

select name,age,addr from user where age

mysql> select name,age,addr from user where addr like '陕西省%';

mysql> select name,age,addr from user where addr like '%市';

mysql> select name,age,addr from user where not (addr like '临汾市');

mysql> select name,age,addr from user where age in (23,25);

mysql> select name,sex,age,addr from user where age not in (15,25);

(15)  查询总数mysql> select count(*) from user;

3.创建CMDB的用户表

建表的sql,性别在数据库中存储的时候,男存1,女存0CREATE TABLE user(

id int primary key auto_increment,

name varchar(32) unique not null default '',

password varchar(512) not null default '',

age int not null default 18,

sex boolean not null default 1,

tel varchar(16) not null default '',

addr text,

add_time datetime

)ENGINE=INNODB DEFAULT CHARSET utf8mb4;

批量插入测试数据insert into user(name, password, age, sex, tel, addr, add_time) values ('kk', md5('kk'), 30, 1, '15200000000', '西安市', now()),\

('woniu', md5('woniu'), 30, 1, '15200000001', '北京市', now()),('zhangzhengguang', md5('zhangzhengguang'), 30, 1, '15200000003', '杭州市', now()),\

('likuan', md5('likuan'), 30, 1, '15200000002', '西安市', now())

查看用户登录的用户名和密码mysql> select name,password from user where name='likuan' and password=md5('likuan');

查找所有的数据mysql> select id,name,password,age,sex,tel,addr from user ;

限制查询的数据 (limit可以用来做分页)mysql> select id,name,password,age,sex,tel,addr from user limit 1;

Limit 和 offset结合使用mysql> select id,name,password,age,sex,tel,addr from user limit 2 offset 2;

排序 (降序和升序)

降序(desc)Mysql> select id,name,password,age,sex,tel,addr from user order by age desc;

升序(asc)mysql> select id,name,password,age,sex,tel,addr from user order by age asc;

更新操作mysql> update user set age=15 where id = 3;

mysql> update user set name='kk',tel='152',sex=1,addr='西安市' where id = 1;

删除操作mysql> delete from user where id = 1;

mysql> delete from user;

聚合函数mysql> select max(age),min(age),avg(age),count(age),sum(age) from user;

分类统计mysql> select addr, count(*) from user group by addr;

mysql> select addr,age, count(*) from user group by addr,age;

4.Python代码里操作mysql

首先需要安装mysql的开发包   mysql-devel

其次pip安装 mysqlclient

使用是导入包  MysqlSQLdb

Python操作mysql的七步

(1)导入模块import MySQLdb

(2)创建连接conn=MySQLdb.connect(host='127.0.0.1',port=3306,user='root',passwd='passwd',db='cmdb')

(3)获取游标cursor = conn.cursor()

(4)执行sql(DQL 和 DML)

DQL

返回符合条件的个数cursor.execute("select id,name from user where name='likuan' and password=md5('likuan');")

DMLcursor.execute("update user set age = 35 where id = 1")

(5)DQL获取结果 、DML提交执行

DQL(元组)cursor.fetchall()

cursor.fetchone()

>>> cursor.fetchall()

(('kk',), ('likuan',), ('woniu',), ('zhangzhengguang',))

DML 提交conn.commit()

(6)关闭游标cursor.close()

(7)关闭连接conn.close()

5.提交sql采用预处理的方式(预防sql注入)

(1)将操作和数据分开

(2)两个变量,一个是sql操作,一个是对应的数据

(3)只有数据才可以占位,操作不能占位

标签:addr,运维,Python,age,Mysql,user,mysql,select,name

来源: https://blog.51cto.com/12217124/2359822

mysql 自动化运维开发_Python自动化运维开发----基础(十三)Mysql数据库基础相关推荐

  1. python实现自动化运维项目_Python自动化运维项目开发最佳实战

    下载地址:五号服务器---VIP资料下载七区\VIP专题教程二区 游客,如果您要查看本帖隐藏内容请回复 Python 自动化运维项目开发最佳实战 这个远比什么老男孩的python好的多 2017-1- ...

  2. python生成二维码_python生成二维码的实例详解

    python生成二维码的实例详解 版本相关 操作系统:Mac OS X EI Caption Python版本:2.7 IDE:Sublime Text 3 依赖库 Python生成二维码需要的依赖库 ...

  3. mysql数据库基础简介_MySql数据库基础之数据库简介及安装

    MySql数据库简介: 众所周知,MySql数据库是一款开源的关系型数据库,在Web应用方面,MySql是最好的.最流行的RDBMS(Relational Database Management Sy ...

  4. python运维开发_Python自动化运维开发----基础(一)

    前言:环境是python3 1.第一个python程序(在学任何一门语言的时候第一程序好像都是hello world),下边我们用python的解释器去输出一个hello world>>& ...

  5. python服务器运维书_python自动化运维书

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  6. python运维系统开发_Python系统运维开发实战

    课程主题: Python 高级运维开发实战 课程讲师: Alex 老师, triaquae python 开源运维管理软件创始人,知名 IT 公司运维开发架构师 课程安排: 每周六一天全天(早 9:0 ...

  7. python 网络接口 开发_Python自动化学习笔记(八)——接口开发、发送网络请求、发送邮件、写日志...

    1.接口开发(flask模块) Python自动化学习笔记(七)接口开发部分的内容补充 1.1参数为json格式: flask.request.is_json #判断参数是否是json格式 flask ...

  8. python自动化开发_python自动化开发-2

    1.python的数据类型之列表 列表是Python开发语言中最常见的数据类型之一,通过列表可以实现对数据的增删改等常用操作. 列表的定义:例子 names = ["Lucy",& ...

  9. python自动化办公实例展示_python自动化办公?学这些就够用了

    知乎上有人提问:用python进行办公自动化都需要学习什么知识呢? 这可能是很多非IT职场人士面临的困惑,想把python用到工作中,却不知如何下手? python在自动化办公领域越来越受欢迎,批量处 ...

最新文章

  1. 入职不到3年,这所C9已有多位“90后”已成副教授!
  2. 怎么制作营销型网站才能有效提升优化效果?
  3. java项目合同制没做完扣钱吗_程序员接私活,怎样防止做完不给钱?
  4. maven如何修改本地仓库与中央仓库
  5. [js] 请使用js实现商品的自由组合,并说说你的思路
  6. FFmpeg在Windows系统下的编译过程
  7. 默认栅格大小为多少_用于创建空栅格的ST_MakeEmptyRaster函数
  8. 不到70行 Python 代码,轻松玩转 RFM 用户分析模型(附案例数据和代码)
  9. Python基础之集合set
  10. ai人工智能_AI破坏已经开始
  11. mui下载 mui.js下载 mui.css下载
  12. 西门子主程序调用子程序_西门子S7-200系列PLC子程序的调用方法
  13. 关于博弈论的硬币问题
  14. postman“在Tests中通过data.token获取token失败”的解决方法
  15. Uvalive 5713 - Qin Shi Huang's National Road System(枚举+最小瓶颈路)
  16. SAT数学:必背公式之三角函数
  17. Objective_C学习笔记
  18. unity tilemap
  19. 谷歌浏览器Chrome错误提示Flash过期怎么办(转)
  20. Gateway服务网关使用教程

热门文章

  1. 史上最全! 全球 22 种开源商业收入模式
  2. 人人都道RAZ好,我读了400多本之后,才明白哪里好
  3. 为什么不建议执行超过3表以上的多表关联查询?
  4. 语音识别(ASR)论文优选:性能测试Wav2Vec2.0 on the Edge: Performance Evaluation
  5. 如何将 MPG 转换为 MP4
  6. 最全长文详述“数字人民币简史”,揭开数字人民币神秘面纱
  7. powerpoint预览_如何调整PowerPoint模板的大小
  8. [WinAPI]通过Windows系统CLSID(GUID)打开系统指定窗口,及部分[上帝模式]命令
  9. 【Web】简单的HTML实现百度搜索
  10. 在Word、WPS中插入AxMath公式导致行间距异常的解决办法