pysql


1.python語言操作mysql的包:

import pymysql
# 連接mysql服務器

conn = pymysql.connect(host='localhost', user='root', password='123',database='db2', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "select * from student where id > %s " % (12,)
# sql =
cursor.execute(sql)

# res = cursor.fetchone()
res = cursor.fetchmany(10)
# res = cursor.fetchall()  ### 列表里面套字典
print(res)

cursor.close()
conn.close()

import pymysql
# 連接mysql服務器

conn = pymysql.connect(host='localhost', user='root', password='123',database='db1', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "delete from t7 where id=3"
cursor.execute(sql)

### 刪除和更新的時候, 需要事物提交
conn.commit()


# res = cursor.fetchone()
# res = cursor.fetchmany(10)
# res = cursor.fetchall() ### 列表里面套字典
# print(res)

cursor.close()
conn.close()
import pymysql
# 連接mysql服務器

conn = pymysql.connect(host='localhost', user='root', password='123',database='db1', charset='utf8')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "delete from t7 where id=3"
cursor.execute(sql)

### 刪除和更新的時候, 需要事物提交
conn.commit()


# res = cursor.fetchone()
# res = cursor.fetchmany(10)
# res = cursor.fetchall() ### 列表里面套字典
# print(res)

cursor.close()
conn.close()




  注:a.文件名不能寫自己本身

    b.connect----->conn----->cursor

    c.執行sql語句  ----->excute(sql)
    d.取數據: fetchone()   取一條 ;    fetchmany()  取多條;   fetchall()取全部

    e:增加刪除:     conn.commit()        *****需要事物提交

    插入一條數據:  cursor.execute('sql,('lxxx,'34234'))

    查如多條數據:      data = [('aaaaa', 'aaa'),('bbbb', 'bbb'),('lxxx,'34234')]

                cursor.execute('sql,data)

2.sol注入

  寫sql語句,%傳值時,要加引號:  sql="select * from t4 where name='%s' and pwd ='%s' "%(username,pwd)

上面那么寫sql語句有風險,eg :
例一: username
= zekai' # select * from t4 where name = 'zekai' #' and pwd = '' 例二: username = dbsahvbdsha' or 1=1 # select * from t4 where name = 'dbsahvbdsha' or 1=1

  我們把這樣的問題稱之為sql 注入,之所以出現這樣 根源是 太過於相信用戶的輸入,導致我們接受用戶傳入的參數時,沒有對其進行轉義;

  解決方案:

    a.自己動手對用戶輸入的值進行轉義;

    b.使用execute()自動進行過濾eg:

        sql = "select * from t4 where name = %s and pwd = %s" 

     cursor.execute(sql,(username, pwd))
3.事物
  定義:一組操作,要me成功,要么失敗
  特性:a.原子性:一組操作要me成功,要么失敗
     b.一致性:事物發生前與發生后 ,數據的 總額 依然 不變 ,能與之匹配
     c.隔離性:簡單說 就是一個事物的操作對其它事物是不可見得
     d.持久性: 當事務完成后, 其影響已形成,不能撤銷,只能通過另一個事物來抵消失誤

場景:
            思考:
                我去銀行給朋友匯款,
                我卡上有1000元,
                朋友卡上500元,
                我給朋友轉賬100元(無手續費),
                如果,網線斷了, 我的錢剛扣,而朋友的錢又沒加時, 怎么辦?
            
            create table t11 (
                id int auto_increment primary key,
                name varchar(32) not null default '',
                money int not null default 0
            )engine=Innodb  charset=utf8;
            
            insert into t11 (name,money) values ('zekai', 1000), ('eagon', 500);
            
        
        解決方法:
        
            開啟事務 (start transaction)
            
            (執行sql操作)
            
            commit : 提交上面的SQL, 讓其生效
            
            rollback: 回滾
            
            show full tables;  顯示全部類型
視圖:
        
        產生的原因:
            如果有一個SQL語句頻繁的會被使用到,比如說:
                select * from t4 where id>12 and id <24;
        
        搞一個映射,或者取一個別名
                select * from t4 where id>12 and id <24   === > v1
        視圖:
        
            select * from v1;
            
        創建視圖:
            
            create view v1 as select * from t4 where id>12 and id <24;
        
        修改視圖:
            alter view v1 as sql語句;
            
        刪除視圖:
            drop view v1;
            
        
        問題:
            如果原生的表數據發生了變化, 那視圖會不會發生變化? 也會變化
            
            視圖中的數據會不會發生修改? 不會發生修改
            
        應用場景:
            
            MySQL: (DBA)
                生成視圖View
            
            程序:
                調用  select  * from v1;
    
    
    
函數:
        
        不要輕易使用
        
        在程序中, 用代碼計算, 計算好了, 再傳給SQL語句執行
    
    
存儲過程:
        
        將一大堆 SQL 語句進行封裝, 類似於函數, 結果就是存儲過程
        
        MySQL服務端:
            DBA (寫)
        
        
        a. 簡單的存儲過程:
            delimiter //
            create procedure p1()
            BEGIN
                select * from t11;
            END //
            delimiter ;
            
            程序:
                call p1();
        
        b. 傳參數: (in)
            delimiter //
            create procedure p2(
                in n1 int,
                in n2 int
            )
            BEGIN
                select * from t11 where id > n1;
            END //
            delimiter ;
            
            程序:
                call p2(12, 2)
        
        c. 傳入參數: (out)
            
            delimiter //
            create procedure p3(
                in n1 int,
                out n2 int
            )
            BEGIN
                select * from t11 where id > n1;
                set n2 =  1;
            END //
            delimiter ;
            
            
            set @v2=123212;
            call p3(12, @v2);
            
            select @v2;
            
            
觸發器:
        
        向用戶表中添加一條數據的同時, 在日志表中也添加一條記錄
        
        delimiter //
        CREATE TRIGGER t1 BEFORE INSERT ON t7 FOR EACH ROW
        BEGIN
            insert into t11 (name, money) values ('xxx', 1234);
        END //
        delimiter ;
        

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM