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)只有數據才能夠佔位,操做不能佔位