当前位置:网站首页>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
边栏推荐
- Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
- 35 pages dangerous chemicals safety management platform solution 2022 Edition
- How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
- 2022 latest and complete interview questions for software testing
- (stinger) use pystinger Socks4 to go online and not go out of the network host
- Container runtime analysis
- JSON data transfer parameters
- Difference between NVIDIA n card and amda card
- How to apply for company email when registering in company email format?
- Agnosticism and practice makes perfect
猜你喜欢

Matlab 信号处理【问答笔记-1】

Bean加载控制

容器运行时分析

Data set - fault diagnosis: various data and data description of bearings of Western Reserve University

Use redis to realize self increment serial number

CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)

Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?

How much do you know about synchronized?

公司里只有一个测试是什么体验?听听他们怎么说吧

2022年最新最全软件测试面试题大全
随机推荐
Maybe you read a fake Tianlong eight
yolov5detect. Py comment
Load balancing cluster (LBC)
请求与响应
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
2022年最新最全软件测试面试题大全
基于FPGA的VGA协议实现
@How to use bindsinstance in dagger2
RTP 接发ps流工具改进(二)
How much do you know about synchronized?
RuntimeError: no valid convolution algorithms available in CuDNN
直击产业落地!飞桨重磅推出业界首个模型选型工具
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
Mapper agent development
sourcetree 详细
Many to one, one to many processing
95 pages of smart education solutions 2022
What can I do after buying a domain name?
Top Devops tool chain inventory