python學習(二十一)加密模塊及數據庫操作


1、MD5加密

不能直接對字符串加密,要先把字符串轉換為bytes類型

import hashlib
m=hashlib.md5()
password='liujia test'
print(password.encode())  #把字符串轉換成bytes類型
m.update(password.encode()) #不能直接對字符串加密,要先把字符串轉換成bytes類型
print(m.hexdigest())

定義函數,做加密

def my_md5(str):
    new_str=str.encode()#把字符串轉換成bytes類型
    #new_str=b'%s'%str #把字符串轉換成bytes類型
    m=hashlib.md5()  #實例化MD5對象
    m.update(new_str)  #加密
    return m.hexdigest()  #獲取結果返回
print(my_md5('jsjs'))

2、其他加密方式

import hashlib
m=hashlib.sha224()  #各種加密方式,用法一樣,只是加密出來的密文不一樣
m.update('adasdjks'.encode())
print(m.hexdigest())

3、操作數據庫

數據庫操作步驟:
1)連接數據庫 賬號,密碼,IP,端口號,數據庫

2)建立游標
3)執行sql
4)獲取結果
5)關閉游標
6)連接關閉

查詢數據庫數據:

import pymysql
coon=pymysql.connect(
    host='xxx.xx.x.x',user='jxz',password='123456',
    port=3306,db='jxz',charset='utf8'
#port必須是int類型,charset必須寫utf8
)
cur=coon.cursor() #建立游標
cur.execute('select * from stu;')
res=cur.fetchall()#獲取所有返回的結果
print(res)
cur.close()  #關閉游標
coon.close() #關閉連接

插入數據:

import pymysql
coon=pymysql.connect(
    host='xxxx',user='jxz',password='123456',
    port=3306,db='jxz',charset='utf8'
#port必須是int類型,charset必須寫utf8
)
cur=coon.cursor() #建立游標
cur.execute('insert into stu(id,name,sex) values(12,"liujia","測試")')
coon.commit()
res=cur.fetchall()#獲取所有返回的結果
print(res)
cur.close()  #關閉游標
coon.close() #關閉連接

4、函數定義數據庫操作

def my_db(host,user,password,db,sql,port=3306,charset='utf8'):
    import pymysql
    coon=pymysql.connect(user=user,
                         host=host,
                         paaaword=password,
                         db=db,
                         charset=charset,
                         port=port
    )
    cur=coon.cursor()
    cur.execute(sql)
    if sql.strip()[:6].upper()=='SELECT':
        res=cur.fetchall()
    else:
        coon.commit()
        res='ok'
    cur.close()
    coon.close()
    return res

5、fetchall() 與fetchone()用法與區別

fetchall() 獲取到sql執行的全部結果,把數據庫里的每一行放到一個list里面
[['1','2','3']]
fetchone 獲取到這個sql執行的一條結果,它返回就只是一條數據
如果sql語句執行的結果是多條,就用fetchall
如果確定執行結果是一條,就用fetchone

6、建立游標指定游標類型,結果為字典類型

def my_db(sql,port=3306,charset='utf8'):
    import pymysql
    host,user,password,db = '118.24.3.40', 'jxz','123456', 'jxz'
    coon=pymysql.connect(user=user,
                         host=host,
                         password=password,
                         db=db,
                         charset=charset,
                         port=port)
    cur=coon.cursor(cursor=pymysql.cursors.DictCursor)
    #建立游標的時候指定了游標類型,返回的就是一個字典
    cur.execute(sql)
    if sql.strip()[:6].upper()=='SELECT':
        res=cur.fetchall()
        print(res)
    else:
        coon.commit()
        res='ok'
    cur.close()
    coon.close()
    return res
my_db('select * from stu')

7、description()用法

獲取數據庫表字典的具體描述,如數據庫表stu中有三個字段,想知道這三個字段的具體描述,如字段類型,長度等

8、取數據庫表頭字段

def my_db(sql,port=3306,charset='utf8'):
    import pymysql
    host,user,password,db = 'xxx', 'jxz','123456', 'jxz'
    coon=pymysql.connect(user=user,
                         host=host,
                         password=password,
                         db=db,
                         charset=charset,
                         port=port)
    cur=coon.cursor(cursor=pymysql.cursors.DictCursor)
    #建立游標的時候指定了游標類型,返回的就是一個字典
    cur.execute(sql)
    if sql.strip()[:6].upper()=='SELECT':
        fileds=[filed[0] for filed in cur.description]  #相當於上面三句
        print(fileds)
    else:
        coon.commit()
        res='ok'
    cur.close()
    coon.close()
    return res
my_db('select * from stu')

 

 

 

 

 


免責聲明!

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



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