Python2 uses mysqldb handouts
This article is mainly introduced to you, hoping to supplement and update some knowledge, if you have other questions you need to know, you can continue to follow my updated article in the industry information.
PyMySQL is a library used to connect to MySQL CVM in the Python3.x version, while mysqldb is used in Python2
Without saying much, go directly to the code to illustrate the purpose.
#! / 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
''
= read mysql configuration in config.ini file =
''
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 ()
''
= encapsulate MySQL basic operation =
''
Class mysqlUtils:
Def _ _ init__ (self):''initialize the mysql connection' '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):''get cursors' self.conn.cursor () def getDict (self,tableName,systemID,ColumnNameKey,ColumnNameValue):''public method Get id dictionary''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):' 'abnormal deletion of data That is, directly operate the database to delete''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):''associate merchants to the XXX industry''# realSQL =" INSERT INTO tableName (system_id,merchant_id, business_id, status, creator_id, create_date, updater_id, update_date) VALUES ('7b6a99f3bce14915863cde5104bdf2c3),% s,' 11customers, 'Aids,' 8customers, unix_timestamp (now ()) * 1000 '813, 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,' 11levels, 'Aids,' 8signals, unix_timestamp (now ()) * 1000, '8levels, unix_timestamp (now ()) * 1000) ", (merchant_id)) self.conn.commit () logger.info ('associate merchant [% s] to xxx success'% merchant_id)
Def close (self):
''
Shut down the mysql database
''
Self.conn.close ()
Read the above handouts on the use of mysqldb in Python2, hoping to give you some help in practical application. Due to the limited space in this article, it is inevitable that there will be deficiencies and need to be supplemented. If you need more professional answers, you can contact us on the official website for 24-hour pre-sales and after-sales to help you answer questions at any time.