首页 > 解决方案 > 从数据库中读取文件会出现错误 TypeError: 'NoneType' 类型的对象没有 len()

问题描述

我正在尝试从表中读取数据(我已存储在数据库中)。当我尝试运行模块时,它给了我错误“TypeError:'NoneType'类型的对象没有len()”。谁能帮帮我吗 ?

代码如下:

import sqlite3
conn = sqlite3.connect('data.db')
c = conn.cursor()    
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

def read_from_db():
    c.execute('SELECT * FROM DATA1')
    data = c.fetchall()
channel.queue_declare(queue = 'hello')
channel.basic_publish(exchange='',
                      routing_key ='hello',
                      body = read_from_db())

print("[x] Sent 'Data'")
connection.close()
c.close()
conn.close()

我还附上了我要阅读的表格图像。在此处输入图像描述

标签: python

解决方案


您的read_from_db函数没有显式返回任何内容,因此当您调用它时,调用者会得到None结果。您将该None值传递给可能发生异常的其他一些库代码(如果您在问题中包含完整的回溯会更容易确定)。

尝试添加return data到函数的末尾。这样你就不会None在你期望其他东西的地方经过。


推荐阅读