python?實現(xiàn)?pymysql?數(shù)據(jù)庫操作方法
更新時間:2022年04月19日 17:34:28 作者:autofelix
這篇文章主要介紹了python實現(xiàn)pymysql數(shù)據(jù)庫操作方法,文章基于python的相關內容展開對?pymysql?數(shù)據(jù)庫操作方法的詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下
一、安裝
pip install pymysql
二、連接數(shù)據(jù)庫
- 三種連接數(shù)據(jù)庫的方式
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ù)庫
- 創(chuàng)建一個test數(shù)據(jù)庫并進入
import pymysql db = pymysql.connect("localhost", "root", "root", "test") # 使用 cursor() 方法創(chuàng)建一個游標對象 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語句 sql = 'INSERT INTO user values("%d","%s")' %(1,"autofelix") cursor.execute(sql) # 提交到數(shù)據(jù)庫執(zhí)行 db.commit() except: # 發(fā)生錯誤時回滾 db.rollback() finally: # 關閉游標連接 cursor.close() # 關閉數(shù)據(jù)庫連接 conn.close()
六、插入多條數(shù)據(jù)
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() try: # 執(zhí)行SQL語句 values = [(1, 'autofelix'), (2, '飛兔小哥')] cursor.executemany('INSERT INTO user values(%s,%s)', values) # 提交到數(shù)據(jù)庫執(zhí)行 db.commit() except: # 發(fā)生錯誤時回滾 db.rollback() finally: # 關閉游標連接 cursor.close() # 關閉數(shù)據(jù)庫連接 conn.close()
七、數(shù)據(jù)統(tǒng)計
import pymysql db = pymysql.connect("localhost", "root", "root", "test") cursor = db.cursor() count = cursor.execute('SELECT * FROM user') # 統(tǒng)計數(shù)據(jù)總數(shù) print('total records: %d' %count) # 統(tǒng)計字段數(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) # 關閉數(shù)據(jù)庫連接 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)
十二、上下文管理
- 每次都連接關閉很麻煩,使用上下文管理,簡化連接過程
import pymysql import contextlib # 定義上下文管理器,連接后自動關閉連接 @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
到此這篇關于python 包 pymysql 數(shù)據(jù)庫操作方法的文章就介紹到這了,更多相關 pymysql 數(shù)據(jù)庫操作 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python設置Pyplot的動態(tài)rc參數(shù)、繪圖的填充
本文主要介紹了python設置Pyplot的動態(tài)rc參數(shù)、繪圖的填充,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06python如何利用paramiko執(zhí)行服務器命令
這篇文章主要介紹了python如何利用paramiko執(zhí)行服務器命令,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11Python+Selenium定位不到元素常見原因及解決辦法(報:NoSuchElementException)
這篇文章主要介紹了Python+Selenium定位不到元素常見原因及解決辦法(報:NoSuchElementException),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03