python3,pymysql,操作MySql數據庫(可直接使用),有的時候執行sql後數據庫卻沒有數據、sql注入漏洞、刪除的風險等等。

不廢話,線上代碼

#__author__ = 'chubby_superman'  胖超人
#_*_coding=utf-8 _*
import pymysql

class db_ope():
    def __init__(self,host,user,password,port,lib):
        self.host=host
        self.user=user
        self.password=password
        self.lib=lib
        self.port=port
    # 連接數據庫
    def get_db(self):
        db = pymysql.connect(self.host,self.user,self.password,self.lib,self.port,charset='utf8')
        return db
    #查詢方法
    def select_func(self,sql):
        database=self.get_db()
        cursor=database.cursor()
        try:
            cursor.execute(sql)
            database.commit()
            data = cursor.fetchone()
        except Exception as e:
            print(e)
            database.rollback()
        print("查詢結果\n",data)
        database.close()
        return data
    #插入方法
    def insertinto_func(self,sql):
        database=self.get_db()
        cursor=database.cursor()
        try:
            cursor.execute(sql)
            database.commit()
            print("保存成功,影響%s行"%cursor.rowcount)
        except Exception as e:
            print(e)
            print("保存失敗")
            database.rollback()
        database.close()
    def delete_func(self,sql):
        database=self.get_db()
        cursor=database.cursor()
        try:
            cursor.execute(sql)
            database.commit()
            print("刪除成功,影響%s行"%cursor.rowcount)
        except Exception as e:
            print(e)
            database.rollback()
        database.close()
    def updata_func(self,sql):
        database=self.get_db()
        cursor=database.cursor()
        try:
            cursor.execute(sql)
            database.commit()
            print("更新成功,影響%s行"%cursor.rowcount)
        except Exception as e:
            print(e)
            database.rollback()
        database.close()

if __name__ == "__main__":
    host = "你家的mysql地址"
    user = "test"
    password = "test1234"
    port = 3306
    lib = "localtest"
    data_base=db_ope(host,user,password,port,lib)
    data_base.select_func("select * from shelf_goods WHERE id=11616")
    '''
    pymysql執行cursor.execute(sql)時可能會存在注入漏洞
    不推薦使用: sql='select * from userinfo where username="%s" and password="%s" ' %(user,pwd,)
    推薦使用  : sql = 'select * from userinfo where username=%s and password=%s',[user,pwd]
    本文可以改成這樣:select_func('select * from userinfo where username=%s and password=%s',[user,pwd])
    '''
    data_base.insertinto_func("INSERT INTO shelf_activity_press(goods_id,goods_name,goods_image,period)"
                                                    " VALUES(11616,'test','a',10000)")
    '''
    1、需要注意,所執行的插入語句依然有可能出問題,例如報出:Warning: (1364, "Field 'period' doesn't have a default value")
    這是因爲,數據表中,相對應的字段沒有默認值,插入時需要設定,否則就會報錯.另外,需要仔細對照字段的數據類型;
    2、有一個小坑,database=self.get_db(),因爲連接數據庫方法,每執行一次,就會建立並返回一個數據庫連接,所以,在執行sql的時候
    應該是這樣:連接數據庫,獲取遊標,遊標執行sql,提交。而不是這樣:連接數據庫,獲取遊標,遊標執行sql,連接數據庫,提交。
    '''

    data_base.updata_func("update shelf_activity_press set goods_name='tester' where period=10000")
    
    data_base.delete_func("DELETE FROM shelf_activity_press WHERE period=10000")
    '''
    1、sql中truncate,delete以及drop的區別,有興趣的童鞋可以百度下,這裏只用delete,從不建議數據庫工程師以外的人
    使用drop和truncate。
    2、sql中,使用刪除時一定要加上where子句哦,否則很容易“被跑路”,也就是辭退,起碼也要通報批評了。
    '''

總結
1、
連接數據庫方法,每執行一次,就會建立並返回一個數據庫連接,有的時候執行sql後數據庫卻沒有數據
應該是這樣:連接數據庫,獲取遊標,遊標執行sql,提交。
而不是這樣:連接數據庫,獲取遊標,遊標執行sql,連接數據庫,提交。
所以,代碼中,調用數據庫連接get_db()後賦值給database,這樣後續操作才能保證在同一鏈接上進行
2、
pymysql執行cursor.execute(sql)時可能會存在注入漏洞
不推薦使用: sql='select * from userinfo where username="%s" and password="%s" ' %(user,pwd,)
推薦使用 : sql = 'select * from userinfo where username=%s and password=%s',[user,pwd]
本文可以改成這樣:select_func('select * from userinfo where username=%s and password=%s',[user,pwd])
3、
sql中truncate,delete以及drop的區別,有興趣的童鞋可以百度下,這裏只用delete,從不建議數據庫工程師以外的人使用drop和truncate。
感興趣的可以看看三者異同:https://www.cnblogs.com/tiger95/p/7466096.html

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