pymysql 增刪改查二次封裝

2021-10-03 14:02:43 字數 3642 閱讀 1985

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @author: joson

# @datetime: 2020/3/6 14:02

# @description: pymysql 增刪改查操作

# @version: 1.0

import pymysql

class

databasehandle

(object):

"""定義mysql資料庫操作類"""

def__init__

(self)

:"""初始化資料庫資訊建立連線"""

self.host =

'host'

self.username =

'root'

self.password =

'123456'

self.database =

'dbname'

self.port =

3306

self.db = pymysql.connect(self.host,self.username,self.password,self.database,self.port,charset=

'utf8'

)def

insertdb

(self,sql)

:"""插入資料"""

self.cursor = self.db.cursor(

)try

: self.cursor.execute(sql)

self.db.commit(

)except exception as err:

print

('insert data error:'

,err)

self.db.rollback(

)# 發生錯誤時回滾

finally

: self.cursor.close(

)def

insertlistdb

(self,table,datalist)

:"""批量插入列表資料

params:

table:插入資料的表名稱

datalist:資料列表 [,...]

"""self.cursor = self.db.cursor(

) cols =

', '

.join(

'`{}`'

.format

(k)for k in datalist[0]

.keys())

val_cols =

', '

.join(

'%({})s'

.format

(k)for k in datalist[0]

.keys())

sql =

'insert into {}(%s) values(%s)'

.format

(table)

res_sql = sql %

(cols, val_cols)

try:

self.cursor.executemany(res_sql, datalist)

# 將字典列表傳入

self.db.commit(

)except exception as err:

print

('insert many data error:'

, err)

self.db.rollback(

)finally

: self.cursor.close(

)def

deletedb

(self,sql)

:"""刪除資料"""

self.cursor = self.db.cursor(

)try

: self.cursor.execute(sql)

self.db.commit(

)except exception as err:

print

('delete data error:'

,err)

self.db.rollback(

)finally

: self.cursor.close(

)def

updatedb

(self,sql)

:"""修改資料"""

self.cursor = self.db.cursor(

)try

: self.cursor.execute(sql)

self.db.commit(

)except exception as err:

print

('update data error:'

,err)

self.db.rollback(

)finally

: self.cursor.close(

)def

selectdb

(self,sql)

:"""查詢資料"""

self.cursor = self.db.cursor(

)# 以元組格式返回查詢結果

# self.cursor = self.db.cursor(cursor=pymysql.cursors.dictcursor) # 查以字典格式返回查詢結果

try:

self.cursor.execute(sql)

data = self.cursor.fetchall(

)except exception as err:

data =

tuple()

print

('select data error:'

,err)

finally

: self.cursor.close(

)return data

defclosedb

(self)

:"""關閉資料庫連線"""

self.db.close(

)if __name__ ==

'__main__'

: dbhandle = databasehandle(

)# dbhandle.insertdb('insert into minitor(id,used_order) values ("999","321")')

# dbhandle.deletedb('delete from minitor where id="999"')

# dbhandle.updatedb('update minitor set used_order="888" where id="999"')

# data = dbhandle.selectdb('select * from minitor where id="999" limit 10')

# datalist = [,]

# dbhandle.insertlistdb('minitor',datalist)

dbhandle.closedb(

)

pymysql的增刪改查操作

1 pymysql模組簡單操作 先pip3 install pymysql 匯入pymysql模組 import pymysql conn pymysql.connect host 127.0.0.1 port 3306 user root password database db5 charset...

如何使用PyMySQL模組進行增刪改查?

mysql 是目前最為流行的關係型資料庫。接下來我們通過乙個簡單的案例,練習如何使用 pymysql 模組進行增刪改查的操作。簡單來講,核心步驟有兩步 鏈結資料庫,讀取 csv 檔案並匯入資料表,然後查詢資料,將資料表和計算結果匯出為 csv 檔案。下面一起來具體看看吧 用pymysql模組增刪改查...

IntelliJ IDEA 增刪改查(二)

建立新的名為dbutill的class 輸入 如圖所示 呼叫 查 把載入驅動 建立連線相關的 換成 把關閉資源相關 換成 增 把載入驅動 建立連線相關的 換成 dbutill dbutill new dbutill connection connection dbutill.getconnectio...