1.MYSQL 语言的分类python
(1) DDL 数据库定义mysql
(2) DQL 数据库查询sql
(3) DML 数据库操做数据库
(4) DCL 数据库权限ide
2.MYSQL 操做
函数
(1) 建立数据库测试
mysql> create database cmdb default charset utf8;
(2)查看全部的数据库fetch
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cmdb | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
(3) 使用cmdb数据库spa
mysql> use cmdb;
(4) 查看数据库的建立语法orm
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 < 25;
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,女存0
CREATE 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');")
DML
cursor.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)只有数据才能够占位,操做不能占位