python - 从数据库中读取文件会出现错误 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()
我还附上了我要阅读的表格图像。在此处输入图像描述
解决方案
您的read_from_db
函数没有显式返回任何内容,因此当您调用它时,调用者会得到None
结果。您将该None
值传递给可能发生异常的其他一些库代码(如果您在问题中包含完整的回溯会更容易确定)。
尝试添加return data
到函数的末尾。这样你就不会None
在你期望其他东西的地方经过。
推荐阅读
- sql - 安全地编写动态 SQL,使用一个示例函数来估计任何视图中的行
- java - 如何在Java中将多对多关系建模为类?
- c++ - 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- javascript - 输入字段为空时如何清除div输出?
- java - Android Studio:从 Run 'app' 安装的应用程序和从 build/outputs/apk/debug/app-debug.apk 安装的应用程序是不同的
- c# - IndentityServer4 客户端权限/声明的 ASP.NET Core API 控制器操作验证
- excel - 如何为同一变量分配更多范围?
- vue.js - 如何通过某些属性从页面中获取相同的 Vue 组件
- javascript - 如何在复选框选中状态和未选中状态下添加和减去列表数据反应本机
- json - CFML/JS 从普通 SQL 创建嵌套的 JSON/数组