首頁 > 軟體

Python中PyMySQL的基本操作

2022-11-07 14:01:02

簡介

PyMySQL 是在 Python3.x 版本中用於連線 MySQL 伺服器的一個庫

PyMySQL 遵循 Python 資料庫 API v2.0 規範,幷包含了 pure-Python MySQL 使用者端庫。

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

pip install PyMySQL

pymysql github地址

下面看下PyMySQL的基本操作,

1、查詢資料

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

# 補充
# 傳入多個資料時
	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)

# 呼叫函數
import pymysql
# 函數已經在mysql資料庫中建立,這裡只呼叫
# 函數的建立請存取後面的網址(https://blog.csdn.net/qq_43102443/article/details/107349451).
# in (指在建立函數時指定的引數只能輸入)
	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()
# 這個函數返回兩個結果集
# 換到另一個結果集,在進行獲取值
	cursor.nextset()
	result_2 = cursor.fetchall()
	cursor.close()
	connect.close()
	
	print('學生:', result_1, 'n老師:', result_2)

# out (指在建立函數時指定的引數只能輸出)
	connect = pymysql.connect(host='localhost', user='root', password='*****', database='schooldb')
	cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
# 呼叫函數
	r = cursor.callproc('p3', (8, 0))
	result_1 = cursor.fetchall()
# 查詢函數的第二個引數的值(從零開始計數)
	cursor.execute('select @_p3_1')
	result_2 = cursor.fetchall()
	cursor.close()
	connect.close()
	
	print(result_1, 'n', result_2)

# inout(指在建立函數時指定的引數既能輸入,又能輸出)
	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、新增資料

用PyMySQL進行資料的增、刪、改時,記得最後要commit()進行提交,這樣才能儲存到資料庫,否則不能

	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、刪除資料

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、更改資料

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操作內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com