sql - python 进行查看和选择数据时出现语法错误

sql - python syntax error making view and selecting data

嗨,希望你玩得开心,我在 python 和 SQL 中开发了一个应用程序,但我在 SQL 中遇到语法错误,你能帮忙吗?

self.mycursor.execute(f'''CREATE VIEW [usable products] AS
            SELECT * FROM products
            WHERE {str(self.counts.value())}> (SELECT count FROM products)
            ''')

错误是:

ight syntax to use near '[usable products] AS
            SELECT * FROM products
            WHER' at line 1

version for the right syntax to use near '[usable products] AS
            SELECT * FROM products
            WHER' at line 1

谢谢!

考虑参数化您的查询并避免 F-string 插值。此外,如果 count(不要误认为 COUNT() 聚合)是 products table.

中的列,则不需要子查询

下面假设您正在使用 pyodbcsqlite3,它们使用 qmarks ? 作为参数占位符。如果使用其他 DB-APIs(psycopg2pymysql 等),请使用 %s 占位符。

sql = (
    "CREATE VIEW [usable products] AS "
    "   SELECT * FROM products "
    "   WHERE count < ?"
)

self.mycursor.execute(sql, [self.counts.value()])