您能帮我解决带变量的Python Sqlite3问题吗?

问题描述 投票:0回答:1

[我正在创建电报机器人,在其中获取电报ID并将其放置在变量中。当我在数据库中对查询使用变量时,出现“ TypeError:函数最多接受2个参数(给定3个)”。

没有查询,变量工作正常。这是一些代码:

import db
from aiogram import types
import expenses
from typing import List, NamedTuple, Optional

class Expense(NamedTuple):
id: Optional[int]
amount: int
category_name: str

def test_last():
    cursor = db.get_cursor()
    asd = types.User.get_current().id #user id 
    cursor.execute("select id, amount, name "
        "from expense,category where codename=category_codename and user_id= ?" ,asd,
        "order by created desc limit 10")
    rows = cursor.fetchall()
    last_expenses = [Expense(id=row[0], amount=row[1], category_name=row[2]) for row in rows]
    return(last_expenses)
python sqlite telegram-bot
1个回答
1
投票

cursor.execute的语法是

  1. 查询字符串
  2. 元组/参数迭代

so

cmd = ("select id, amount, name from expense,category where " +
       "codename=category_codename and user_id= ? " +
       "order by created desc limit 10")

cursor.execute(cmd, (asd,) )

应该工作。

这里是备忘单(sqlite使用?)http://bobby-tables.com/python

© www.soinside.com 2019 - 2024. All rights reserved.