当前位置:网站首页>Define MySQL function to realize multi module call
Define MySQL function to realize multi module call
2022-07-02 23:51:00 【Your baby seven】
import pymysql
class my_sql:
def __init__(self,host,database,user,password,port=3306):
""" :param host: # The host we want to connect ip Address If it is a local machine, fill it in localhost perhaps 127.0.0.1 Enter the corresponding network address ip :param database: # database Enter the database we want to operate :param user: # Database account The default is root :param password: # Database password :param port: # The default port is 3306 If the replacement Please fill in the port number after replacement """
try:
self.con = pymysql.connect(
host=host,
database=database,
port=port,
user=user,
password=password,
cursorclass=pymysql.cursors.DictCursor # A row of data is in the form of a dictionary . Multi row data is a dictionary nested in a list .
)
except :
raise
else:
# Create cursors
self.cur = self.con.cursor()
def get_count_query(self,select_sql,args=None):
count =self.cur.execute(select_sql,args)
return count
def get_quert_sql_result(self,select_sql,args=None,size=1):
""" :param select_sql: :param args: :param size: size = 1 Express fetchone size = -1 Express fetchall size>1 Express fetchmany :return: """
self.cur.execute(select_sql,args)
if size == 1 :
return self.cur.fetchone()
elif size == -1 :
return self.cur.fetchall()
elif size >1:
return self.cur.fetchmany(size)
def update_sql(self,update_sql,args=None):
try:
self.cur.execute(update_sql,args)
except :
self.con.rollback()
else:
self.con.commit()
def colse(self):
self.cur.close()
self.con.close()
if __name__ == '__main__':
sql_tuple = ("127.0.0.1","root","root","123456",3306)
# Define a tuple to pass in our database connection information And pass it into *args Magic variable
mysql = my_sql(*sql_tuple)
res = mysql.get_quert_sql_result('SELECT * FROM member WHERE mobile_phone="12345678900";')
# Call the query method to check the data
print(res)
mysql.update_sql('UPDATE member SET leave_amount=1314 WHERE mobile_phone="12345678900";')
# Call the update method to modify the data
res = mysql.get_quert_sql_result('SELECT leave_amount FROM member WHERE mobile_phone="12345678900";')
# Call the query method to check the data Has the update been completed
print(res)
mysql.colse()
# Close connection pool Release resources
边栏推荐
- 返回二叉树中最大的二叉搜索子树的根节点
- 开发知识点
- Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
- MFC gets the current time
- C# MVC创建一个视图摆脱布局的影响
- Returns the root node of the largest binary search subtree in a binary tree
- Wechat applet basic learning (wxss)
- 35 pages dangerous chemicals safety management platform solution 2022 Edition
- Flexible combination of applications is a false proposition that has existed for 40 years
- Go basic data type
猜你喜欢

程序分析与优化 - 9 附录 XLA的缓冲区指派

JDBC practice cases
![[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)](/img/93/dc940caebe176177e4323317ebf4fa.jpg)
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)

【ML】李宏毅三:梯度下降&分类(高斯分布)

Maybe you read a fake Tianlong eight

95页智慧教育解决方案2022

What can I do after buying a domain name?

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

接口差异测试——Diffy工具

数据集-故障诊断:西储大学轴承的各项数据以及数据说明
随机推荐
Optimization of streaming media technology
Speech recognition Series 1: speech recognition overview
附加:token;(没写完,别看…)
All things work together, and I will review oceanbase's practice in government and enterprise industry
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
yolov5detect. Py comment
Installing redis under Linux
List of major chip Enterprises
流媒体技术优化
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
【STL源码剖析】仿函数(待补充)
Program analysis and Optimization - 9 appendix XLA buffer assignment
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
VIM interval deletion note
Flexible combination of applications is a false proposition that has existed for 40 years
Solution to boost library link error
How much do you know about synchronized?
Agnosticism and practice makes perfect
Returns the root node of the largest binary search subtree in a binary tree