1. 连接数据库
#coding:utf-8
import mysql.connector #python2使用的是import MySQLdb.cursors
class OperationMysql:
def __init__(self):
self.conn = mysql.connector.connect(
host='xxx.xxx.xxx.xxx',
port=3306,
user='???',
passwd='???',
db='???',
charset='utf8'
)
self.cur = self.conn.cursor(dictionary=True) #传递dictionary,返回dict类型
def search_one(self,sql):
self.cur.execute(sql)
result = self.cur.fetchone()
return result
2. 操作数据库
if __name__ == '__main__':
op_mysql = OperationMysql()
result1 = op_mysql.search_one("select * from xt_yhzh where zhmc='Zhaowenjun'")
print(result1)
3. 结果
![](https://img.haomeiwen.com/i9034168/28925870def8075a.png)
传递dictionary的结果
![](https://img.haomeiwen.com/i9034168/e312f6753340033c.png)
不传递dictionary的结果
网友评论