pymysql模塊的數據操做

數據的簡單查詢操做:mysql

 1 import pymysql  2 username=input("請輸入用戶名:")  3 pwd=input("請輸入密碼:")  4 
 5 conn=pymysql.connect(host="localhost",user="kevin",password='12121',database="around") #建立鏈接
 6 cursor=conn.cursor()  #建立一個遊標對象
 7 sql="select * from userinfo where user=%s AND psd=%s"
 8 r=cursor.execute(sql,[username,pwd])   #受影響的行數
 9 print(r) 10 ret=cursor.fetchone()      #查詢到的結果
11 print(ret) 12 
13 cursor.close()   #關閉遊標
14 conn.close() 

添加數據操做:sql

 1 import pymysql  2 conn=pymysql.connect(host="localhost",user="kevin",password="12121",database='around')  3 cursor=conn.cursor()  4 sql="insert into userinfo(user,psd) VALUES (%s,%s)"
 5 r=cursor.executemany(sql,[('jack','1234'),('lisa','12138'),('time','11594')]) #插入多行記錄時必須用executemany  6 conn.commit()  7 print(r)  8 
 9 cursor.close() 10 conn.close()

 建立視圖:(不推薦使用)數據庫

1 CREATE VIEW cont AS
2 SELECT s.uid 編號,s.uname 用戶名,s.psd 密碼,s.powid 權限號,power.cate 權限 FROM 
3     (SELECT * FROM userinfo INNER JOIN control ON userinfo.uid=control.userid)AS s INNER JOIN power
4     ON power.pid=s.powid

觸發器的建立:函數

1     USE around; 2     delimiter //    -- 觸發器
3     CREATE TRIGGER t1 BEFORE INSERT ON userinfo FOR EACH ROW   -- 在執行userinfo插入一條數據後會被執行
4     BEGIN
5     INSERT INTO student(sname)VALUES(NEW.user); 6     END //
7  delimiter ; 8     
9     INSERT INTO userinfo (user,psd)VALUES('abc','admin');

>>>>>>>>>>>>>>>>>>>>>>>>>>>>由於SQL語句默認識別分號爲結束標誌,因此觸發器在建立時必須修改默認標誌,建立完成以後再改回原樣fetch

存儲過程的實現:ui

MySQL端代碼塊:spa

 1 delimiter //
 2     CREATE PROCEDURE p1 (IN t1 INT,OUT t2 INT )  3     BEGIN
 4        SET t2=12121;  5      SELECT * FROM student WHERE sid > t1;  6          INSERT INTO teacher (tname)VALUES('小芬');  7   END //
 8  delimiter ;  9     
10     SET @v=0; 11     call p1(10,@v); 12     SELECT @v;

pycharm端代碼塊:code

 1 import pymysql  2 
 3 conn=pymysql.connect(host='localhost',user='kevin',password='12121',database='around',charset='utf8')  4 cursor=conn.cursor()  5 cursor.callproc('p1',(10,123))  6 conn.commit()  7 ret=cursor.fetchall()  8 print(ret)  9 
10 cursor.execute('select @_p1_0,@_p1_1') 11 r=cursor.fetchall() 12 print(r) 13 
14 cursor.close() 15 conn.close()

用存儲過程實現把A表的數據id和num求和插入B表:對象

建立的A表和B表:blog

 

1     CREATE TABLE A(id INT PRIMARY KEY auto_increment, 2     number INT); 3 
4     CREATE TABLE B(bid INT PRIMARY KEY auto_increment, 5     num INT);

 

MySQL代碼塊:

 1     delimiter //
 2     CREATE PROCEDURE pre()  3     BEGIN
 4     DECLARE row_id INT;           -- 自定義變量
 5     DECLARE row_num INT;   -- 自定義變量
 6     DECLARE done INT DEFAULT FALSE;      -- done 默認爲false
 7     DECLARE temp int;  8     
 9     DECLARE my_cursor CURSOR FOR SELECT id,number FROM A;  -- 定義一個遊標去取值
10     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;  -- 若是爲true 則遊標數據爲空
11     
12     OPEN my_cursor; 13  xxx: LOOP 14                     FETCH my_cursor INTO row_id,row_num;    -- 獲取的值傳給變量
15                     IF done THEN
16  LEAVE xxx; 17                     END IF; 18                     SET temp=row_id+row_num; 19                     INSERT INTO B (num)VALUES(temp); 20           END LOOP xxx; 21     END //
22     delimiter ;

 >>>>>>>>>>>>>>>>>>>>>>>>>>>

數據庫 動態防SQL注入:

1     delimiter //
2     CREATE PROCEDURE proce( IN nid INT) 3     BEGIN
4      SET @nid=nid; 5          PREPARE prod FROM 'select * FROM student WHERE sid > ?'; 6          EXECUTE prod USING @nid; 7          DEALLOCATE PREPARE prod; 8     END  //
9     delimiter  ;

建立索引:

1  -- 索引的建立
2 CREATE INDEX ix_name ON student(sname); 3  -- 索引的刪除
4 DROP INDEX ix_name ON student; 5 索引種類(某種格式存儲):hash索引: 單值快 6 btree索引: btree索引二叉樹  適合範圍查詢

 索引的命中:

 1 - like '%xx'
 2                 select * from tb1 where email like '%cn';  3                 
 4                 
 5             - 使用函數  6                 select * from tb1 where reverse(email) = 'wupeiqi';  7                 
 8                 
 9             - or
10                 select * from tb1 where nid = 1 or name = 'seven@live.com'; 11                 
12                 
13  特別的:當or條件中有未創建索引的列才失效,如下會走索引 14                         select * from tb1 where nid = 1 or name = 'seven'; 15                         select * from tb1 where nid = 1 or name = 'seven@live.com' and email = 'alex'
16                         
17                         
18             - 類型不一致 19  若是列是字符串類型,傳入條件是必須用引號引發來,否則... 20                 select * from tb1 where email = 999; 21                 
22                 
23             - !=
24                 select * from tb1 where email != 'alex'
25                 
26  特別的:若是是主鍵,則仍是會走索引 27                     select * from tb1 where nid != 123
28             - >
29                 select * from tb1 where email > 'alex'
30                 
31                 
32  特別的:若是是主鍵或索引是整數類型,則仍是會走索引 33                     select * from tb1 where nid > 123
34                     select * from tb1 where num > 123
35                     
36                     
37             - order by
38                 select name from tb1 order by email desc; 39                 
40  當根據索引排序時候,選擇的映射若是不是索引,則不走索引 41  特別的:若是對主鍵排序,則仍是走索引: 42                     select * from tb1 order by nid desc; 43              
44             - 組合索引最左前綴 45  若是組合索引爲:(name,email) 46                 name and email       -- 使用索引
47                 name                 -- 使用索引
48                 email                -- 不使用索引

 注意事項:

1 避免使用select  *  來直接查詢

2.用count(1)或者count(列)代替count(*)

3.表的固定長度字段優先

4.建立表時儘可能使用char代替varchar

5.組合索引替代多個單列索引

6.儘可能使用短索引

7.使用join鏈接來代替子查詢

8.連表是注意條件類型一致

9.重複或者少值散列的列不適合創建索引 好比性別

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>執行計劃:讓mysql預估執行操做(通常正確) explain

 2    執行效率:慢--->快    all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const
 3             id,email
 4             
 5             慢:
 6                 select * from userinfo3 where name='alex'
 7                 
 8                 explain select * from userinfo3 where name='alex'
 9                 type: ALL(全表掃描)
10                     select * from userinfo3 limit 1;
11             快:
12                 explain select * from userinfo3 where email='alex'
13                 type: const(走索引)

 分頁操做:

 1 ******分頁*******
 2         
 3         a. select * from userinfo3 limit 20,10;  4  b.  5             - 不讓看  6             - 索引表中掃:  7                 select * from userinfo3 where id in(select id from userinfo3 limit 200000,10)  8             - 方案:  9  記錄當前頁最大或最小ID 10                 1. 頁面只有上一頁,下一頁 11  # max_id 12  # min_id 13  下一頁: 14                         select * from userinfo3 where id > max_id limit 10; 15  上一頁: 16                         select * from userinfo3 where id < min_id order by id desc limit 10; 17                 2. 上一頁 192 193  [196]  197  198  199 下一頁 18                     
19                     select * from userinfo3 where id in ( 20                         select id from (select id from userinfo3 where id > max_id limit 30) as N order by N.id desc limit 10
21                     )
相關文章
相關標籤/搜索