首页 > 解决方案 > psycopg2.errors.SyntaxError:“C”或附近的语法错误

问题描述

hwid1 = str(subprocess.check_output(
    'wmic csproduct get uuid')).split('\\r\\n')[1].strip('\\r').strip() # Get Hard Ware Id of the pc



def AutoUpdateDB():
    SQL.execute(f"select hwid from Accounts WHERE hwid = {hwid1}")
    result_user = SQL.fetchone()
    print(result_user[0])
    time.sleep(5555)

我正在尝试这段代码,它给了我错误
我试图解决这个问题,但它仍然无法正常工作

这是错误

psycopg2.errors.SyntaxError: "C"
LINE 1 处或附近的语法错误: ...ect * from Accounts WHERE hwid = AD902276-A4F9-961C-492B-2CF ...

                                                         <br/>^

标签: pythonpsycopg2

解决方案


其他人已经指出了为什么你会得到你得到的特定错误,但我想提一下,解决它的方法几乎总是使用参数化查询,即类似

SQL.execute("SELECT hwid FROM Accounts WHERE hwid = %s", [hwid1])

如果 hwid1 包含“有趣”字符,这可以为您省去很多麻烦。


推荐阅读