第二百七十九节,MySQL数据库-pymysql模块操作数据库
MySQL數(shù)據(jù)庫-pymysql模塊操作數(shù)據(jù)庫
pymysql模塊是python操作數(shù)據(jù)庫的一個模塊
?
connect()創(chuàng)建數(shù)據(jù)庫鏈接,參數(shù)是連接數(shù)據(jù)庫需要的連接參數(shù)
使用方式:
模塊名稱.connect()
參數(shù):
host=數(shù)據(jù)庫ip
port=數(shù)據(jù)庫端口
user=數(shù)據(jù)庫用戶名
passwd=數(shù)據(jù)庫密碼
db=數(shù)據(jù)庫名稱
charset=數(shù)據(jù)庫編碼
cursor()創(chuàng)建數(shù)據(jù)庫操作游標,無參
使用方式:
游標變量.cursor()
execute()操作數(shù)據(jù)庫,參數(shù)1 sql語句,參數(shù)2 字符串占位符變量
使用方式:
游標變量.execute()
execute()操作數(shù)據(jù)庫會返回,操作數(shù)據(jù)庫后影響的行數(shù),我們可以以此判斷是否操作成功
commit()提交數(shù)據(jù)到數(shù)據(jù)庫,無參
使用方式:
創(chuàng)建數(shù)據(jù)庫鏈接變量.commit()
close()關(guān)閉游標
使用方式:
游標變量.close()
close()關(guān)閉數(shù)據(jù)庫
使用方式:
創(chuàng)建數(shù)據(jù)庫變量.close()
向數(shù)據(jù)庫添加一條數(shù)據(jù)
?
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi') # 創(chuàng)建游標 cursor = conn.cursor()# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.execute("INSERT INTO db1(yhm,mim) VALUES('adc8868','279819')") #添加一條數(shù)據(jù) print(effect_row) #返回影響行數(shù)# 提交,不然無法保存新建或者修改的數(shù)據(jù) conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
?
execute(sql語句%s,(占位符變量))執(zhí)行sql語句時的占位符使用
execute()操作一條數(shù)據(jù)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi') # 創(chuàng)建游標 cursor = conn.cursor()# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.execute("INSERT INTO db1(yhm,mim) VALUES(%s,%s)",('adc279819',279819)) #添加一條數(shù)據(jù) print(effect_row) #返回影響行數(shù)# 提交,不然無法保存新建或者修改的數(shù)據(jù) conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
?
executemany(sql語句,[(占位符變量),(占位符變量)])執(zhí)行sql語句時的占位符使用
executemany()操作多條數(shù)據(jù)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi') # 創(chuàng)建游標 cursor = conn.cursor()# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.executemany("INSERT INTO db1(yhm,mim) VALUES(%s,%s)",[('a1',123),('a2',456),('a3',789)]) print(effect_row) #返回影響行數(shù)# 提交,不然無法保存新建或者修改的數(shù)據(jù) conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
?
查詢數(shù)據(jù)庫數(shù)據(jù)
注意:操作數(shù)據(jù)庫的增、刪、改都需要commit()提交數(shù)據(jù)到數(shù)據(jù)庫,而查詢是不需要commit()的
fetchall()獲取游標查詢數(shù)據(jù)庫里的數(shù)據(jù),返回元祖
使用方式:
游標變量.fetchall()
?
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 charset=數(shù)據(jù)庫編碼 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi',charset='utf8') # 創(chuàng)建游標 cursor = conn.cursor()# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.execute("SELECT id,yhm,mim FROM db1") shuju = cursor.fetchall() #獲取游標里的數(shù)據(jù) print(shuju) # 提交,不然無法保存新建或者修改的數(shù)據(jù) # conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
?
?
?
查詢數(shù)據(jù)庫內(nèi)容時更改游標返回字典類型數(shù)據(jù)【推薦】
返回字典類型將數(shù)據(jù)庫表的列(字段)作為鍵返回
默認查詢數(shù)據(jù)時游標返回的元祖類型,如果想返回字典類型就需要設置游標
cursor=pymysql.cursors.DictCursor設置游標返回字典類型數(shù)據(jù),當做參數(shù)寫在execute()里,execute(cursor=pymysql.cursors.DictCursor)
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 charset=數(shù)據(jù)庫編碼 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi',charset='utf8') # 創(chuàng)建游標 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.execute("SELECT id,yhm,mim FROM db1") shuju = cursor.fetchall() #獲取游標里的數(shù)據(jù) print(shuju)# 提交,不然無法保存新建或者修改的數(shù)據(jù) # conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
?
fetchone()獲取游標里第一條數(shù)據(jù),如果多次執(zhí)行fetchone()就依次獲取數(shù)據(jù)
使用方式:
游標變量.fetchone()
?
?
fetchmany()獲取游標里,指定條數(shù)據(jù),參數(shù)是要獲取數(shù)據(jù)的條數(shù)
使用方式:
游標變量.fetchmany(3)
?
?
移動游標里數(shù)據(jù)指針獲取對應數(shù)據(jù)
scroll(1,mode='relative')相對當前位置移動
使用方式:
游標變量.scroll(1,mode='relative')
第一個參數(shù)正數(shù)相對當前位置向下移動數(shù)值對應指針,第一個負數(shù)相對當前位置向上移動數(shù)值對應指針
scroll(2,mode='absolute')相對絕對位置移動
使用方式:
游標變量.scroll(2,mode='absolute')
將指針位置移動到第一個參數(shù)數(shù)值對應指針
相對當前位置移動獲取數(shù)據(jù)mode='relative'
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 charset=數(shù)據(jù)庫編碼 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi',charset='utf8') # 創(chuàng)建游標 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.execute("SELECT id,yhm,mim FROM db1") shuju = cursor.fetchone() #獲取游標里第一條數(shù)據(jù),指針在第一個位置 shuju = cursor.fetchone() #指針在第二個位置 shuju = cursor.fetchone() #指針在第三個位置 print(shuju) #打印應該是第三條 cursor.scroll(-2,mode='relative') #調(diào)整指針,相對當前位置向上移動2位 shuju = cursor.fetchone() #指針第二位 print(shuju) # 提交,不然無法保存新建或者修改的數(shù)據(jù) # conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
相對絕對位置移動mode='absolute'
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql# 創(chuàng)建連接 """ host=數(shù)據(jù)庫ip port=數(shù)據(jù)庫端口 user=數(shù)據(jù)庫用戶名 passwd=數(shù)據(jù)庫密碼 db=數(shù)據(jù)庫名稱 charset=數(shù)據(jù)庫編碼 """ conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='279819', db='cshi',charset='utf8') # 創(chuàng)建游標 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 執(zhí)行SQL,并返回收影響行數(shù) effect_row = cursor.execute("SELECT id,yhm,mim FROM db1") shuju = cursor.fetchone() #獲取游標里第一條數(shù)據(jù),指針在第一個位置 shuju = cursor.fetchone() #指針在第二個位置 shuju = cursor.fetchone() #指針在第三個位置 print(shuju) #打印應該是第三條 cursor.scroll(3,mode='absolute') #調(diào)整指針,相對絕對位置移動3位 shuju = cursor.fetchone() #指針第四位 print(shuju) # 提交,不然無法保存新建或者修改的數(shù)據(jù) # conn.commit()# 關(guān)閉游標 cursor.close() # 關(guān)閉連接 conn.close()?
?
添加數(shù)據(jù)庫時獲取到添加數(shù)據(jù)的自增id
lastrowid獲取添加數(shù)據(jù)時的自增id
使用方式:
游標變量.lastrowid
注意:如果是添加的多條數(shù)據(jù),獲取到的自增id是最后一條的自增id
?
轉(zhuǎn)載于:https://www.cnblogs.com/adc8868/p/6942542.html
總結(jié)
以上是生活随笔為你收集整理的第二百七十九节,MySQL数据库-pymysql模块操作数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven+SSM框架工程搭建
- 下一篇: Ubuntu 中Mysql 操作