当前位置:网站首页>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
边栏推荐
- Arduino - character judgment function
- Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
- MySQL基础
- Where is the win11 microphone test? Win11 method of testing microphone
- [shutter] shutter open source project reference
- yolov5train. py
- How much do you know about synchronized?
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- Integration of revolution and batch normalization
- Sourcetree details
猜你喜欢

JDBC練習案例

Load balancing cluster (LBC)

What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it

CDN acceleration requires the domain name to be filed first

How much do you know about synchronized?

【STL源码剖析】仿函数(待补充)

What can I do after buying a domain name?

JDBC Exercise case

Fusion de la conversion et de la normalisation des lots

List of major chip Enterprises
随机推荐
Speech recognition Series 1: speech recognition overview
php 获取真实ip
实用系列丨免费可商用视频素材库
Judge whether the binary tree is full binary tree
yolov5test. Py comment
Mapper代理开发
95页智慧教育解决方案2022
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
RuntimeError: no valid convolution algorithms available in CuDNN
Program analysis and Optimization - 9 appendix XLA buffer assignment
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
VIM interval deletion note
Optimization of streaming media technology
Dishes launcher small green program and directory management (efficiency tool)
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
How to apply for company email when registering in company email format?
开发知识点
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north