python?實(shí)現(xiàn)?pymysql?數(shù)據(jù)庫(kù)操作方法
一、安裝
pip install pymysql
二、連接數(shù)據(jù)庫(kù)
- 三種連接數(shù)據(jù)庫(kù)的方式
import pymysql # 方式一 conn = pymysql.connect('localhost', 'root', 'root') # 方式二 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='', charset='utf8') # 方式三 config = { 'host': '127.0.0.1', 'port': 3306, 'user': 'root', 'passwd': 'root', 'charset': 'utf8' } conn = pymysql.connect(**config)
三、創(chuàng)建數(shù)據(jù)庫(kù)
- 創(chuàng)建一個(gè)test數(shù)據(jù)庫(kù)并進(jìn)入
import pymysql db = pymysql.connect("localhost", "root", "root", "test") # 使用 cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor cursor = db.cursor() cursor.execute('DROP DATABASE IF EXISTS test') cursor.execute('CREATE DATABASE IF NOT EXISTS test') conn.select_db('test')
四、創(chuàng)建數(shù)據(jù)表
- 創(chuàng)建一張user表
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() cursor.execute('CREATE TABLE user(id int primary key,name varchar(30))')
五、插入一條數(shù)據(jù)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() try: # 執(zhí)行SQL語(yǔ)句 sql = 'INSERT INTO user values("%d","%s")' %(1,"autofelix") cursor.execute(sql) # 提交到數(shù)據(jù)庫(kù)執(zhí)行 db.commit() except: # 發(fā)生錯(cuò)誤時(shí)回滾 db.rollback() finally: # 關(guān)閉游標(biāo)連接 cursor.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close()
六、插入多條數(shù)據(jù)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() try: # 執(zhí)行SQL語(yǔ)句 values = [(1, 'autofelix'), (2, '飛兔小哥')] cursor.executemany('INSERT INTO user values(%s,%s)', values) # 提交到數(shù)據(jù)庫(kù)執(zhí)行 db.commit() except: # 發(fā)生錯(cuò)誤時(shí)回滾 db.rollback() finally: # 關(guān)閉游標(biāo)連接 cursor.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close()
七、數(shù)據(jù)統(tǒng)計(jì)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() count = cursor.execute('SELECT * FROM user') # 統(tǒng)計(jì)數(shù)據(jù)總數(shù) print('total records: %d' %count) # 統(tǒng)計(jì)字段數(shù) print('total records:', cursor.rowcount)
八、獲取表名信息
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() desc = cursor.description print("%s %3s" % (desc[0][0], desc[1][0]))
九、獲取單條數(shù)據(jù)
- 使用 fetchone 方法獲取單條數(shù)據(jù)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() # 使用 execute() 方法執(zhí)行 SQL 查詢 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法獲取單條數(shù)據(jù). data = cursor.fetchone() print("Database version : %s " % data) # 關(guān)閉數(shù)據(jù)庫(kù)連接 db.close()
十、查詢多條數(shù)據(jù)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() cursor.execute('SELECT * FROM user') results = cursor.fetchmany(5) for r in results: print (r)
十一、查詢所有數(shù)據(jù)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() cursor.execute('SELECT * FROM user') results = cursor.fetchall() for r in results: print (r)
十二、上下文管理
- 每次都連接關(guān)閉很麻煩,使用上下文管理,簡(jiǎn)化連接過(guò)程
import pymysql import contextlib # 定義上下文管理器,連接后自動(dòng)關(guān)閉連接 @contextlib.contextmanager def mysql(host='127.0.0.1', port=3306, user='root', passwd='', db='test',charset='utf8'): conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset=charset) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) try: yield cursor finally: conn.commit() cursor.close() conn.close() # 執(zhí)行sql with mysql() as cursor: print(cursor) count = cursor.execute("select * from user") row_1 = cursor.fetchone() print row_count, row_1
到此這篇關(guān)于python 包 pymysql 數(shù)據(jù)庫(kù)操作方法的文章就介紹到這了,更多相關(guān) pymysql 數(shù)據(jù)庫(kù)操作 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
3行Python代碼實(shí)現(xiàn)剪輯音樂(lè)
你以為剪輯音樂(lè)要很久嗎?其余3行語(yǔ)句Python就能瞬間搞定。本文就來(lái)詳細(xì)為大家講講實(shí)現(xiàn)的步驟,文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手嘗試一下2022-06-06python設(shè)置Pyplot的動(dòng)態(tài)rc參數(shù)、繪圖的填充
本文主要介紹了python設(shè)置Pyplot的動(dòng)態(tài)rc參數(shù)、繪圖的填充,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06jupyter notebook遠(yuǎn)程訪問(wèn)不了的問(wèn)題解決方法
這篇文章主要介紹了jupyter notebook遠(yuǎn)程訪問(wèn)不了的問(wèn)題解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01一款強(qiáng)大的端到端測(cè)試工具Playwright介紹
這篇文章主要為大家介紹了一款強(qiáng)大的端到端測(cè)試工具Playwright介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01python如何利用paramiko執(zhí)行服務(wù)器命令
這篇文章主要介紹了python如何利用paramiko執(zhí)行服務(wù)器命令,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11Python+Selenium定位不到元素常見(jiàn)原因及解決辦法(報(bào):NoSuchElementException)
這篇文章主要介紹了Python+Selenium定位不到元素常見(jiàn)原因及解決辦法(報(bào):NoSuchElementException),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03基于python實(shí)現(xiàn)銀行管理系統(tǒng)
這篇文章主要介紹了基于python實(shí)現(xiàn)銀行管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python項(xiàng)目制作的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04python區(qū)塊鏈實(shí)現(xiàn)簡(jiǎn)版工作量證明
這篇文章主要為大家介紹了python區(qū)塊鏈實(shí)現(xiàn)簡(jiǎn)版工作量證明詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05