python pymysql庫的常用操作
更新時(shí)間:2020年10月16日 08:50:25 作者:Virya
這篇文章主要介紹了python pymysql庫的常用操作,幫助大家更好的利用python操作數(shù)據(jù)庫,感興趣的朋友可以了解下
批量插入
import pymysql
def insert_to_mysql(to_db_list):
mysql_db = pymysql.connect(host="HOST_IP", port=3306, user="username", password="password",
database="db", charset="utf8")
cursor = mysql_db.cursor()
sql = "INSERT INTO `your_db`.`your_table`(`colum1`, `colum2`, `colum3`) VALUES (%s,%s,%s)"
try:
# cursor.execute()
cursor.executemany(sql, to_db_list) # 批量插入
effect_rows = cursor.rowcount
mysql_db.commit()
cursor.close()
print('數(shù)據(jù)庫添加成功,插入 {}條數(shù)據(jù)'.format(effect_rows))
return effect_rows
except Exception as e:
mysql_db.rollback()
print('數(shù)據(jù)庫執(zhí)行失敗')
print(e)
return 0
my_list = []
my_list.append(('v1', 'v2', 'v3'))
cnt = insert_to_mysql(my_list)
查詢
def get_id_name(): cursor = mysql_db.cursor() sql = "select id, name from `your_db`.`table`" cursor.execute(sql) res = cursor.fetchall() # print(res) return res my_list = get_id_name() for index in range(len(my_list)): print(my_list[index][0]) # id print(my_list[index][1]) # name
更新
def update_by_id(update_list):
"""根據(jù)ID更新col1, col2, col3
list 依次為 col1, col2, col3, id
:param update_list:
:return:
"""
cursor = mysql_db.cursor()
sql = "UPDATE `your_db`.`table` SET col1=(%s),col2=(%s),col3=(%s) WHERE id=(%s)"
try:
# cursor.execute()
cursor.executemany(sql, update_list) # 批量插入
mysql_db.commit()
cursor.close()
print('數(shù)據(jù)庫更新成功')
except Exception as e:
mysql_db.rollback()
print('數(shù)據(jù)庫更新失敗')
print(e)
my_list = []
my_list.append(('v1', 'v2', 'v3', 'id'))
update_by_id(my_list)
以上就是python pymysql庫的常用操作的詳細(xì)內(nèi)容,更多關(guān)于python pymysql庫的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
- 利用python中pymysql操作MySQL數(shù)據(jù)庫的新手指南
- Python接口自動化淺析pymysql數(shù)據(jù)庫操作流程
- python使用pymysql模塊操作MySQL
- pymysql實(shí)現(xiàn)增刪改查的操作指南(python)
- python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫
- Python pymysql模塊安裝并操作過程解析
- python數(shù)據(jù)庫操作mysql:pymysql、sqlalchemy常見用法詳解
- 在python中使用pymysql往mysql數(shù)據(jù)庫中插入(insert)數(shù)據(jù)實(shí)例
- Python使用pymysql模塊操作mysql增刪改查實(shí)例分析
- python之pymysql模塊簡單應(yīng)用示例代碼
- wxpython+pymysql實(shí)現(xiàn)用戶登陸功能
- 在Python中使用MySQL--PyMySQL的基本使用方法
- Python 中使用 PyMySQL模塊操作數(shù)據(jù)庫的方法
- 使用python連接mysql數(shù)據(jù)庫之pymysql模塊的使用
- Python pymysql操作MySQL詳細(xì)
相關(guān)文章
一篇文章帶你學(xué)習(xí)Python3的高級特性(1)
這篇文章主要為大家詳細(xì)介紹了Python3的高階函數(shù),主要介紹什么是高級特性,高級特性的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解Python和Rust中內(nèi)存管理機(jī)制的實(shí)現(xiàn)與對比
Python和Rust都采用了垃圾收集(Garbage?Collection)機(jī)制來管理內(nèi)存,但它們各自的實(shí)現(xiàn)方式有很大的不同,下面就跟隨小編一起來深入了解下二者的區(qū)別吧2024-03-03
Python?Bleach保障網(wǎng)絡(luò)安全防止網(wǎng)站受到XSS(跨站腳本)攻擊
Bleach?不僅可以清理?HTML?文檔,還能夠?qū)︽溄舆M(jìn)行處理,檢查是否是合法格式,并可以使用白名單來控制哪些?HTML?標(biāo)簽、屬性是安全的,因此非常適合用于清潔用戶輸入的數(shù)據(jù),確保網(wǎng)站安全2024-01-01
Python中selenium_webdriver下拉框操作指南
selenium 雖然過了這么多年,但是到目前為止依然是比較流行的自動化框架了,下面這篇文章主要給大家介紹了關(guān)于Python中selenium_webdriver下拉框操作的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
基于keras中import keras.backend as K的含義說明
這篇文章主要介紹了keras中import keras.backend as K的含義說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
詳解?PyTorch?Lightning模型部署到生產(chǎn)服務(wù)中
這篇文章主要為大家介紹了如何將PyTorch?Lightning模型部署到生產(chǎn)服務(wù)中的詳細(xì)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

