美文网首页
Python3操作数据库读取sql语句报错:TypeError:

Python3操作数据库读取sql语句报错:TypeError:

作者: Queenie的学习笔记 | 来源:发表于2019-12-11 11:56 被阅读0次

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

    图片.png

    3. 原因

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

    相关文章

      网友评论

          本文标题:Python3操作数据库读取sql语句报错:TypeError:

          本文链接:https://www.haomeiwen.com/subject/rcsogctx.html