通过合并2个表写入sqlite 3数据库

2024-06-16 11:14:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两张桌子,顾客和课程。我想用正确的客户id将日期和时间写入我的课程表的正确行中。我想使用客户登录时输入的用户名从客户表中获取客户id,然后使用该客户id将日期和时间插入正确的位置。这是我的密码:

with sqlite3.connect('sqlite.db') as db:
            c = db.cursor()
    custid = c.execute('SELECT customerid FROM customers WHERE customerusernm = @username2')
    print(custid)
    c.execute('INSERT INTO lessons(lessondt,lessontm) VALUES (dt,tm) WHERE custid = customerid')
    connect.commit()
    connect.close()

我在select语句的行中遇到此错误:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.

我如何解决这个问题,有没有更好的方法


Tags: idexecutedb客户connect时间wheresqlite3
1条回答
网友
1楼 · 发布于 2024-06-16 11:14:34

您必须提供一个元组,该元组的值应绑定在您编写“@username2”的位置。请尝试以下操作:

with sqlite3.connect('sqlite.db') as db:
            c = db.cursor()
    # set the variable to the username you want to compare to
    username = 'some username'
    custid = c.execute('SELECT customerid FROM customers WHERE customerusernm = @username2', (username,))
    print(custid)
    c.execute('INSERT INTO lessons(lessondt,lessontm) VALUES (dt,tm) WHERE custid = customerid')
    connect.commit()
    connect.close()

根据检索用户名的方式,可以使用getpass模块:

import getpass    

username = getpass.getuser()

相关问题 更多 >