1. 代碼部分
#coding:utf-8
import mysql.connector
import json
from MyEncoder import MyEncoder
class OperationMysql:
def __init__(self):
self.conn = mysql.connector.connect(
host='xxx.xx.xxx.xxx',
port=3306,
user='xxxxxxxx',
passwd='xxxxxxxxxx',
db='xxxxxxx',
charset='utf8'
)
self.cur = self.conn.cursor(dictionary=True) #self代表全局,#傳遞dictionary扁眯,返回dict類型
self.cur = self.conn.cursor()
def search_one(self,sql):
self.cur.execute(sql)
result = self.cur.fetchone() #fetchonone
return result[0]
#result_json = json.dumps(result,cls=MyEncoder) #
#return result_json
if __name__ == '__main__':
op_mysql = OperationMysql()
result1 = op_mysql.search_one("select sessionId from xt_login_log where usercode = (select ygxxid from xt_yhzh where zhmc = 'huangxiaoming') and operationType = 1 ORDER BY operationTime DESC LIMIT 1")
#result1 = op_mysql.search_one("select * from xt_ygxxb where ygxm = '葛文娟' and jgid = '3dbe692399ba449294a44d27d0e085e1'")
#print(type(result1))
print(result1)
2. 報錯信息
3. 原因
'NoneType' 是指返回的內(nèi)容是None空對象,說明sql執(zhí)行的語句返回結(jié)果為空硅蹦,原因在于這條sql語句查詢的內(nèi)容不存在荣德,而我代碼連接的測試環(huán)境數(shù)據(jù)庫中不存在“葛文娟”這個用戶,所以找不到提针。