Python通過pymysql連接數據庫並進行查詢和更新SQL方法封裝
import 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)