where python报错_Python3操作数据库读取sql语句报错:TypeError: 'NoneType' object is not subscriptable...

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. 报错信息

71b6b2c9d5f4

图片.png

3. 原因

'NoneType' 是指返回的内容是None空对象,说明sql执行的语句返回结果为空,原因在于这条sql语句查询的内容不存在,而我代码连接的测试环境数据库中不存在“葛文娟”这个用户,所以找不到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值