最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中PyMySQL的基本操作

 更新時間:2022年11月07日 10:20:36   作者:尛刀石  
PyMySQL?遵循?Python?數(shù)據(jù)庫?API?v2.0?規(guī)范,并包含了?pure-Python?MySQL?客戶端庫,這篇文章主要介紹了Spring?DI依賴注入詳解,需要的朋友可以參考下

簡介

PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務器的一個庫

PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫。

如果還未安裝,我們可以使用以下命令安裝最新版的 PyMySQL:

pip install PyMySQL

pymysql github地址

下面看下PyMySQL的基本操作,

1、查找數(shù)據(jù)

import pymysql
# 簡單的查找
# 連接數(shù)據(jù)庫
	conn = pymysql.connect(host='127.0.0.1', user='root', password='******', database='authoritydb')
# cursor=pymysql.cursors.DictCursor,是為了將數(shù)據(jù)作為一個字典返回
	cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
	sql = 'select id,name from userinfo where id = %s'
# row返回獲取到數(shù)據(jù)的行數(shù)
# 不能將查詢的某個表作為一個參數(shù)傳入到SQL語句中,否則會報錯
# eg:sql = 'select id,name from %s'
# eg:row = cursor.excute(sql, 'userinfo') # 備注:userinfo是一個表名
# 像上面這樣做就會報SQL語法錯誤,正確做法如下:
	row = cursor.execute(sql, 1)
# fetchall()(獲取所有的數(shù)據(jù)),fetchmany(size)(size指定獲取多少條數(shù)據(jù)),fetchone()(獲取一條數(shù)據(jù))
	result = cursor.fetchall()
	cursor.close()
	conn.close()
# 最后打印獲取到的數(shù)據(jù)
	print(result)

# 補充
# 傳入多個數(shù)據(jù)時
	sql = 'select id,name from userinfo where id>=%s and id<=%s'
	row = cursor.executemany(sql, [(1, 3)])
# 以字典方式傳值
	sql = 'select id,name from userinfo where id>=%(id)s or name=%(name)s'
	rows = cursor.execute(sql, {'id': id, 'name': name})
	
# -------------------------------------------------
	import pymysql
# 復雜一點的查找,與MySQL的語句格式一樣
	connect = pymysql.connect(host='localhost', user='root', password='****', database='authoritydb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
	username = input('username: ')
	sql = 'select A.name,authorityName from (select name,aid from userauth left join userinfo on' \
	      ' uid=userinfo.id where name=%s) as A left join authority on authority.id=A.aid'
	row = cursor.execute(sql, username)
	result = cursor.fetchmany(row)
	cursor.close()
	connect.close()
	print(result)

# 調(diào)用函數(shù)
import pymysql
# 函數(shù)已經(jīng)在mysql數(shù)據(jù)庫中創(chuàng)建,這里只調(diào)用
# 函數(shù)的創(chuàng)建請訪問后面的網(wǎng)址(https://blog.csdn.net/qq_43102443/article/details/107349451).
# in (指在創(chuàng)建函數(shù)時指定的參數(shù)只能輸入)
	connect = pymysql.connect(host='localhost', user='root', password='******', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
	r = cursor.callproc('p2', (10, 5))
	result_1 = cursor.fetchall()
# 這個函數(shù)返回兩個結果集
# 換到另一個結果集,在進行獲取值
	cursor.nextset()
	result_2 = cursor.fetchall()
	cursor.close()
	connect.close()
	
	print('學生:', result_1, '\n老師:', result_2)

# out (指在創(chuàng)建函數(shù)時指定的參數(shù)只能輸出)
	connect = pymysql.connect(host='localhost', user='root', password='*****', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
# 調(diào)用函數(shù)
	r = cursor.callproc('p3', (8, 0))
	result_1 = cursor.fetchall()
# 查詢函數(shù)的第二個參數(shù)的值(從零開始計數(shù))
	cursor.execute('select @_p3_1')
	result_2 = cursor.fetchall()
	cursor.close()
	connect.close()
	
	print(result_1, '\n', result_2)

# inout(指在創(chuàng)建函數(shù)時指定的參數(shù)既能輸入,又能輸出)
	connect = pymysql.connect(host='localhost', user='root', password='l@l19981019', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
	r = cursor.callproc('p4', (10, 0, 2))
	result_1 = cursor.fetchall()
	cursor.execute('select @_p4_1,@_p4_2')
	result_2 = cursor.fetchall()
	cursor.close()
	connect.close()
	print(result_1, '\n', result_2)

2、添加數(shù)據(jù)

用PyMySQL進行數(shù)據(jù)的增、刪、改時,記得最后要commit()進行提交,這樣才能保存到數(shù)據(jù)庫,否則不能

	connect = pymysql.Connect(host='localhost', user='root', password='******', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
	student_id, course_id, number = input('student_id,course_id,number[eg:1 2 43]: ').split()
	sql = 'insert into score(student_id,course_id,number) values(%(student_id)s,%(course_id)s,%(number)s)'
	rows = cursor.execute(sql,{'student_id': student_id, 'course_id': course_id, 'number': number})
# 這里一定要提交
	cursor.commit()
	cursor.close()
	connect.close()

3、刪除數(shù)據(jù)

connect = pymysql.Connect(host='localhost', user='root', password='******', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
	student_id = input('student_id: ')
	sql = 'delete from student where sid=%s'
	rows = cursor.execute(sql, student_id)
# 這里一定要提交
	cursor.commit()
	cursor.close()
	connect.close()

4、更改數(shù)據(jù)

connect = pymysql.Connect(host='localhost', user='root', password='******', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
	id, name = input('id, name: ').split()
	sql = "update userinfo set name=%s where id=%s"
	rows = cursor.executemany(sql, [(name, id)])
# 這里一定要提交
	cursor.commit()
	cursor.close()
	connect.close()

到此這篇關于PyMySQL的基本操作的文章就介紹到這了,更多相關PyMySQL操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 對python numpy數(shù)組中冒號的使用方法詳解

    對python numpy數(shù)組中冒號的使用方法詳解

    下面小編就為大家分享一篇對python numpy數(shù)組中冒號的使用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python spilt()分隔字符串的實現(xiàn)示例

    python spilt()分隔字符串的實現(xiàn)示例

    split() 方法可以實現(xiàn)將一個字符串按照指定的分隔符切分成多個子串,本文介紹了spilt的具體使用,感興趣的可以了解一下
    2021-05-05
  • Python將list中的string批量轉化成int/float的方法

    Python將list中的string批量轉化成int/float的方法

    今天小編就為大家分享一篇Python將list中的string批量轉化成int/float的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 使用Python獲取愛奇藝電視劇彈幕數(shù)據(jù)的示例代碼

    使用Python獲取愛奇藝電視劇彈幕數(shù)據(jù)的示例代碼

    這篇文章主要介紹了用Python獲取愛奇藝電視劇彈幕數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python實現(xiàn)的矩陣轉置與矩陣相乘運算示例

    Python實現(xiàn)的矩陣轉置與矩陣相乘運算示例

    這篇文章主要介紹了Python實現(xiàn)的矩陣轉置與矩陣相乘運算,結合實例形式分析了Python針對矩陣進行轉置與相乘運算的相關實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下
    2019-03-03
  • Python使用os.listdir和os.walk獲取文件路徑

    Python使用os.listdir和os.walk獲取文件路徑

    這篇文章主要介紹了Python使用os.listdir和os.walk獲取文件路徑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • python搭建項目的虛擬環(huán)境

    python搭建項目的虛擬環(huán)境

    本文主要介紹了python搭建項目的虛擬環(huán)境,主要就是在多個項目中進行一個隔離,防止包的版本沖突或者其他情況,下面就來介紹一下具體步驟,感興趣的可以了解一下
    2024-03-03
  • Python處理mat文件的三種方式小結

    Python處理mat文件的三種方式小結

    這篇文章主要介紹了Python處理mat文件的三種方式小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • PyCharm搭建Spark開發(fā)環(huán)境的實現(xiàn)步驟

    PyCharm搭建Spark開發(fā)環(huán)境的實現(xiàn)步驟

    這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Python實現(xiàn)雙色球號碼隨機生成

    Python實現(xiàn)雙色球號碼隨機生成

    和體彩大樂透類似,福彩雙色球也是購買次數(shù)最多的彩種之一,相比大樂透,雙色球更容易中小獎。本文將介紹?Python?實習雙色球彩票自由的流程,感興趣的可以了解一下
    2022-05-05

最新評論

绍兴县| 澳门| 丰顺县| 昌宁县| 沧州市| 邵阳县| 浦县| 阿尔山市| 康乐县| 阜新市| 大悟县| 宾阳县| 太仓市| 明星| 观塘区| 黄龙县| 彭山县| 灵宝市| 莱阳市| 翼城县| 高碑店市| 维西| 房产| 东莞市| 大石桥市| 石楼县| 天柱县| 东海县| 当雄县| 沂南县| 于都县| 策勒县| 阿拉善左旗| 嘉义县| 蕲春县| 巨鹿县| 许昌县| 濉溪县| 嘉禾县| 股票| 莱阳市|