Python中PyMySQL的基本操作
簡介
PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務器的一個庫
PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫。
如果還未安裝,我們可以使用以下命令安裝最新版的 PyMySQL:
pip install PyMySQL
下面看下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)
# 調用函數(shù)
import pymysql
# 函數(shù)已經(jīng)在mysql數(shù)據(jù)庫中創(chuàng)建,這里只調用
# 函數(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)
# 調用函數(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操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
對python numpy數(shù)組中冒號的使用方法詳解
下面小編就為大家分享一篇對python numpy數(shù)組中冒號的使用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python spilt()分隔字符串的實現(xiàn)示例
split() 方法可以實現(xiàn)將一個字符串按照指定的分隔符切分成多個子串,本文介紹了spilt的具體使用,感興趣的可以了解一下2021-05-05
Python將list中的string批量轉化成int/float的方法
今天小編就為大家分享一篇Python將list中的string批量轉化成int/float的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
使用Python獲取愛奇藝電視劇彈幕數(shù)據(jù)的示例代碼
這篇文章主要介紹了用Python獲取愛奇藝電視劇彈幕數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Python使用os.listdir和os.walk獲取文件路徑
這篇文章主要介紹了Python使用os.listdir和os.walk獲取文件路徑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05
PyCharm搭建Spark開發(fā)環(huán)境的實現(xiàn)步驟
這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09

