如何修复此错误?(Python Sqlite3)

xzabzqsa  于 2022-11-14  发布在  SQLite
关注(0)|答案(2)|浏览(159)
from typing import NewType
import sqlite3

connection = sqlite3.connect("uids.db")
cursor = connection.cursor()
table = """CREATE TABLE USERS (
           UID INT
           USERNAME VARCHAR(25) NOT NULL
           );"""
cursor.execute("DROP TABLE IF EXISTS USERS")
cursor.execute(table)
print("table ok")





uid = NewType('uid', int)

def ucheck(id: uid) -> int:
    if id <= 18446744073709551615:
        print("uid good")
        return 0
    else:
        raise ValueError("UID not less than or equal to 64 bit signed integer limit")
        return 1

def ucreate(idx: uid, usrname: str):
    cursor.execute(f"""INSERT INTO USERS VALUES ({uid}, ?)""", (usrname))
    print(f"USER WITH NAME {usrname} AND ID {uid} CREATED")

ucreate(1, "admin")

当我运行此代码时,它返回以下错误:

table ok
Traceback (most recent call last):
  File "main.py", line 35, in <module>
    ucreate(1, "admin")
  File "main.py", line 31, in ucreate
    cursor.execute(f"""INSERT INTO USERS VALUES ({uid}, ?)""", (usrname))
sqlite3.OperationalError: near "<": syntax error

有谁可以帮我?我对SQLITE还不太熟悉
我尝试更改引号,尝试使用?运算符,并将参数放在后面,但仍然不起作用。

b4qexyjb

b4qexyjb1#

UID是类型名称。如果要使用插值法,应该是

cursor.execute(f"""INSERT INTO USERS VALUES ({idx}, ?)""", (usrname,))
hgc7kmma

hgc7kmma2#

对两个变量都使用占位符,而不仅仅是usrname。正确的变量是idx

def ucreate(idx: uid, usrname: str):
    cursor.execute(f"""INSERT INTO USERS VALUES (?, ?)""", (idx, usrname))
    print(f"USER WITH NAME {usrname} AND ID {uid} CREATED")

相关问题