Python自動化運維開發----基礎(十三)Mysql數據庫基礎

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;

image.png

(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;

image.png

(11)  插入數據

mysql> insert into user(id,name,age,sex,telphone,addr)values(1,'李寬',25,1,'18829787559','陝西省西安市');

(12)  查看數據

mysql> select * from user;

image.png

(13)  只查詢指定的列

mysql> select name,addr from user;

image.png

(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;

image.png

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

image.png

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

image.png

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

image.png

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

image.png

select name,age,addr from user where age < 25;

image.png

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

image.png

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

image.png

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

image.png

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

image.png

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

image.png

(15)  查詢總數

mysql> select count(*) from user;

image.png

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');

image.png

查找所有的數據

mysql> select id,name,password,age,sex,tel,addr from user ;

image.png

限制查詢的數據 (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;

image.png

排序 (降序和升序)

降序(desc)

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

image.png

升序(asc)

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

image.png

更新操作

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;

image.png

分類統計

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

image.png

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

image.png

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)只有數據纔可以佔位,操作不能佔位

image.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章