当前位置:Gxlcms >
数据库问题 >
Python通过pymysql连接数据库并进行查询和更新SQL方法封装
Python通过pymysql连接数据库并进行查询和更新SQL方法封装
时间:2021-07-01 10:21:17
帮助过:85人阅读
pymysql.cursors
import json
class OperationMysql:
def __init__(self):
self.conn =
pymysql.connect(
host=
‘127.0.0.1‘,
port=3306
,
user=
‘test‘,
passwd=
‘11111‘,
db=
‘test‘,
charset=
‘utf8‘,
cursorclass=
pymysql.cursors.DictCursor
)
self.cur =
self.conn.cursor()
# 查询一条数据
def search_one(self, sql):
self.cur.execute(sql)
result =
self.cur.fetchone()
return result
# 更新SQL
def updata_one(self, sql):
self.cur.execute(sql)
self.conn.commit()
self.conn.close()
if __name__ ==
‘__main__‘:
op_mysql =
OperationMysql()
res = op_mysql.search_one(
"SELECT * from order WHERE order_no=‘M191023401681654‘")
print(res)
Python通过pymysql连接数据库并进行查询和更新SQL方法封装
标签:token json int mys passwd hone connect 修改 etc