Home > Enterprise >  How do I fix this error? (Python SQLITE3)
How do I fix this error? (Python SQLITE3)

Time:11-02

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")
        



When I run this code, it returns this error:

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

Can anyone help me? I am kind of new to SQLITE

I tried changing the quotations, I tried using the ? operator and putting the parameters after and it still did not work.

CodePudding user response:

uid is type name. If you want to use interpolation, should be

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

CodePudding user response:

Use placeholders for both variables, not just usrname. And the correct variable is 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")
  • Related