Python3操作MySQL数据库(简化)
时间:2021-07-01 10:21:17
帮助过:22人阅读
pymysql
class SQLHepler:
def __init__(self
, USER
, PASSWORD
, DATABASE
, HOST
=‘127.0.0.1‘, CHARSET
=‘utf8‘):
self
.HOST
= HOST
self
.USER
= USER
self
.PASSWORD
= PASSWORD
self
.DATABASE
= DATABASE
self
.CHARSET
= CHARSET
def get_conn(self
):
conn
= pymysql
.connect
(host
=self
.HOST
, user
=self
.USER
, password
=self
.PASSWORD
, db
=self
.DATABASE
, charset
=self
.CHARSET
)
cur
= conn
.cursor
()
return conn
, cur
‘‘‘
功能:单向操作,主要用于(增加,删除,修改)
参数:安全的sql语句
‘‘‘
def get_excute_Non_query(self
, sql
):
conn
, cur
= self
.get_conn
()
try:
cur
.execute
(sql
)
conn
.commit
()
except:
print(‘查询失败‘)
conn
.rollback
()
conn
.close
()
‘‘‘
功能:查询数据库中数据
参数:安全的sql语句
‘‘‘
def get_date_query(self
, sql
):
conn
, cur
= self
.get_conn
()
data
= None
try:
cur
.execute
(sql
)
data
= cur
.fetchall
()
except:
print(‘查询失败‘)
conn
.close
()
return data
原文:大专栏 Python3操作MySQL数据库(简化)
Python3操作MySQL数据库(简化)
标签:简化 -name 服务器 import close operator ase triple database