欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中PyMySQL的基本操作

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

簡介

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

PyMySQL 遵循 Python 數據庫 API v2.0 規(guī)范,并包含了 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數據庫中創(chuàng)建,這里只調用
# 函數的創(chuàng)建請訪問后面的網址(https://blog.csdn.net/qq_43102443/article/details/107349451).
# in (指在創(chuàng)建函數時指定的參數只能輸入)
	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 (指在創(chuàng)建函數時指定的參數只能輸出)
	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(指在創(chuàng)建函數時指定的參數既能輸入,又能輸出)
	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操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python實現手機通訊錄搜索功能

    python實現手機通訊錄搜索功能

    這篇文章主要介紹了python模仿手機通訊錄搜索功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python數據分析基礎之異常值檢測和處理方式

    Python數據分析基礎之異常值檢測和處理方式

    這篇文章主要介紹了Python數據分析基礎之異常值檢測和處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Django傳遞數據給前端的3種方式小結

    Django傳遞數據給前端的3種方式小結

    Django從后臺往前臺傳遞數據時有多種方法可以實現,下面這篇文章主要給大家介紹了關于Django傳遞數據給前端的3種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • 基于python不同開根號的速度對比分析

    基于python不同開根號的速度對比分析

    這篇文章主要介紹了基于python不同開根號的速度對比分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python中Django的URL反向解析

    Python中Django的URL反向解析

    這篇文章主要介紹了Python中Django的URL反向解析,url反向解析是指在視圖或模板中,用path定義的名稱來動態(tài)查找或計算出相應的路由,本文提供了部分實現代碼與解決思路,需要的朋友可以參考下
    2023-09-09
  • pandas中.loc和.iloc以及.at和.iat的區(qū)別說明

    pandas中.loc和.iloc以及.at和.iat的區(qū)別說明

    這篇文章主要介紹了pandas中.loc和.iloc以及.at和.iat的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 在python中使用[[v]*n]*n遇到的坑及解決

    在python中使用[[v]*n]*n遇到的坑及解決

    這篇文章主要介紹了在python中使用[[v]*n]*n遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 不要用強制方法殺掉python線程

    不要用強制方法殺掉python線程

    本文給大家分享的是走著的一些強制殺掉python線程經驗教訓,如果你使用強制手段干掉線程,那么很大幾率出現意想不到的bug。 請記住一點,鎖資源不會因為線程退出而釋放鎖資源 !
    2017-02-02
  • Python第三方庫OS庫方法實操

    Python第三方庫OS庫方法實操

    這篇文章主要給大家介紹了關于Python第三方庫OS庫的相關資料,os庫主要是對文件和文件夾進行操作,在Python中對?件和?件夾的操作要借助os模塊??的相關功能,需要的朋友可以參考下
    2024-06-06
  • python+Tkinter+多線程的實例

    python+Tkinter+多線程的實例

    這篇文章主要介紹了python+Tkinter+多線程的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評論