您好,登錄后才能下訂單哦!
本文主要給大家介紹,希望可以給大家補(bǔ)充和更新些知識(shí),如有其它問(wèn)題需要了解的可以持續(xù)在億速云行業(yè)資訊里面關(guān)注我的更新文章的。
PyMySQL 是在 Python3.x 版本中用于連接 MySQL 云服務(wù)器的一個(gè)庫(kù),Python2中則使用mysqldb
話(huà)不多說(shuō),直接上代碼來(lái)說(shuō)明用途
#!/user/bin/env python
#coding=utf-8
from pymysql import connect,cursors
from pymysql.err import OperationalError
import os
import configparser as cparser
from builtins import int
from framework.logger import Logger
import time
'''
========讀取config.ini文件中mysql配置========
'''
base_dir = str(os.path.dirname(os.path.dirname(file)))
file_path = base_dir + "\config\config.ini"
cf = configUtil(file_path)
host = cf.get("sitmysqlconf", "host")
port = cf.get("sitmysqlconf", 'port')
db = cf.get("sitmysqlconf", 'db_name')
user = cf.get("sitmysqlconf", 'user')
password = cf.get("sitmysqlconf", 'password')
logger = Logger(logger="mysqlUtils").getlog()
'''
===========封裝MySQL基本操作=============
'''
class mysqlUtils:
def __init__(self):
'''
初始化獲得mysql連接
'''
try:
self.conn = connect(host=host,
port=int(port),
user=user,
password=password,
db=db,
charset='utf8mb4',
cursorclass=cursors.DictCursor
)
except OperationalError as e:
print (e)
def cursor(self):
'''
獲得游標(biāo)
'''
self.conn.cursor()
def getDict(self,tableName,systemID,ColumnNameKey,ColumnNameValue):
'''
公共方法,獲取id的字典
'''
with self.conn.cursor() as cursor:
cursor.execute("select * from %s WHERE system_id = %s and %s = %s",(tableName,systemID,ColumnNameKey,ColumnNameValue))
Dict = cursor.fetchone()
self.conn.commit()
return Dict
def AttentionLibraryDelete(self,system_id,merchant_id):
'''非正常刪除數(shù)據(jù),即直接操作數(shù)據(jù)庫(kù)刪除'''
with self.conn.cursor() as cursor:
cursor.execute("delete from tableName where system_id = %s and merchant_id = %s;",(system_id,merchant_id))
self.conn.commit()
def addMerchantTOIT(self,merchant_id):
'''把商家關(guān)聯(lián)到XXX行業(yè)中'''
#realSQL = "INSERT INTO tableName (system_id, merchant_id, business_id, status, creator_id, create_date, updater_id, update_date) VALUES ('7b6a99f3bce14915863cde5104bdf2c3', %s, '11', 'A', '8', unix_timestamp(now())*1000, '8', unix_timestamp(now())*1000);" % repr(merchant_id)
with self.conn.cursor() as cursor:
cursor.execute("INSERT INTO t_sys_merchant_business (system_id, merchant_id, business_id, status, creator_id, create_date, updater_id, update_date) VALUES ('7b6a99f3bce14915863cde5104bdf2c3', %s, '11', 'A', '8', unix_timestamp(now())*1000, '8', unix_timestamp(now())*1000);",(merchant_id))
self.conn.commit()
logger.info('把商家【%s】關(guān)聯(lián)到xxx成功'%merchant_id)
def close(self):
'''
關(guān)閉mysql數(shù)據(jù)庫(kù)
'''
self.conn.close()
看了以上關(guān)于Python2使用mysqldb講義,希望能給大家在實(shí)際運(yùn)用中帶來(lái)一定的幫助。本文由于篇幅有限,難免會(huì)有不足和需要補(bǔ)充的地方,如有需要更加專(zhuān)業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時(shí)售前售后,隨時(shí)幫您解答問(wèn)題的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。