当前位置:网站首页>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
边栏推荐
- Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
- A single element in an ordered array -- Valentine's Day mental problems
- Installing redis under Linux
- Go basic data type
- JDBC Exercise case
- Interface switching based on pyqt5 toolbar button -1
- MySQL Foundation
- Why can't the start method be called repeatedly? But the run method can?
- [ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
- Bean加载控制
猜你喜欢
How much do you know about synchronized?
JDBC教程
What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
I've been interviewed. The starting salary is 16K
数据集-故障诊断:西储大学轴承的各项数据以及数据说明
Implementation of VGA protocol based on FPGA
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
JDBC練習案例
随机推荐
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
程序分析与优化 - 9 附录 XLA的缓冲区指派
[analysis of STL source code] imitation function (to be supplemented)
跨境电商如何通过打好数据底座,实现低成本稳步增长
可知论与熟能生巧
JDBC tutorial
MySQL Foundation
公司里只有一个测试是什么体验?听听他们怎么说吧
How to set automatic reply for mailbox and enterprise mailbox?
C MVC creates a view to get rid of the influence of layout
附加:token;(没写完,别看…)
Go basic constant definition and use
接口自动化覆盖率统计——Jacoco使用
Returns the maximum distance between two nodes of a binary tree
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
How much do you know about synchronized?
yolov5train. py
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Installing redis under Linux
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception