You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLite表行数据存入二维码后扫描无法提取数据的问题

二维码扫描仅显示[]的问题解决办法

嘿,我看你遇到的问题是:明明存入二维码的数据是完整的[(1, 'YELLOW', '2019-12-30 17:42:42.971372')],但扫描的时候却只显示[]。这大概率是因为你直接把fetchall()返回的列表元组转成字符串后,二维码扫描器对这种Python风格的格式解析有问题,或是扫描器的显示逻辑只识别到了外层括号。

下面给你几个可行的解决方案:

1. 转换成用户友好的文本格式(最简单)

把查询结果拆成单独字段,用易懂的格式拼接,扫描后能直接看到清晰的信息:

def qr_yellow(sl):
    # 改用参数化查询避免SQL注入风险!
    c.execute("SELECT * from ColorDetector where sl = ?", (sl,))
    result = c.fetchall()
    
    if result:
        # sl是主键,只会返回一条数据,直接解构字段
        sl_id, color, timestamp = result[0]
        # 用换行和清晰标签拼接内容
        qr_data = f"颜色ID: {sl_id}\n颜色: {color}\n记录时间: {timestamp}"
    else:
        qr_data = "未找到对应颜色数据"
    
    url = pyqrcode.create(qr_data)
    url.svg(f"CodeYellow{sl}.svg", scale=8)

扫描后会显示:

颜色ID: 1
颜色: YELLOW
记录时间: 2019-12-30 17:42:42.971372

2. 转换成JSON格式(推荐,适合后续解析)

JSON是通用的结构化数据格式,扫描后不仅能完整显示,还能轻松解析成字典用于后续处理:

import json

def qr_yellow(sl):
    c.execute("SELECT * from ColorDetector where sl = ?", (sl,))
    result = c.fetchall()
    
    if result:
        sl_id, color, timestamp = result[0]
        # 封装成字典再转成JSON字符串
        data = {
            "sl": sl_id,
            "color": color,
            "timestamp": timestamp
        }
        qr_data = json.dumps(data, ensure_ascii=False)
    else:
        qr_data = json.dumps({"error": "未找到数据"}, ensure_ascii=False)
    
    url = pyqrcode.create(qr_data)
    url.svg(f"CodeYellow{sl}.svg", scale=8)

扫描后会得到:
{"sl":1,"color":"YELLOW","timestamp":"2019-12-30 17:42:42.971372"}

3. 修复原始格式的显示问题

如果一定要保留类似元组的格式,你可以只取第一个结果(而非整个列表)转成字符串,去掉外层方括号:

def qr_yellow(sl):
    c.execute("SELECT * from ColorDetector where sl = ?", (sl,))
    result = c.fetchall()
    
    if result:
        # 只取第一条数据转字符串,避免外层列表的[]干扰
        qr_data = str(result[0])
    else:
        qr_data = "No data found"
    
    url = pyqrcode.create(qr_data)
    url.svg(f"CodeYellow{sl}.svg", scale=8)

这样存入二维码的是(1, 'YELLOW', '2019-12-30 17:42:42.971372'),扫描器应该能完整显示整个内容。

额外提醒

  • 一定要注意SQL注入风险!你原来用format拼接SQL语句的方式很危险,上面的代码都改成了参数化查询(用?占位符),这是Python操作SQLite的最佳实践。
  • 可以换用主流扫描工具测试,比如微信、支付宝的扫码功能,有些小众扫描器可能对特殊字符的支持不太好。

内容的提问来源于stack exchange,提问作者Gautham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:05:35