oracledb.exceptions.DatabaseError:ORA-00911:无效字符,但当我在Oracle中运行SQL成功[重复]

vdgimpew  于 2023-08-04  发布在  Oracle
关注(0)|答案(1)|浏览(119)

此问题在此处已有答案

ORA-00911 Error only with oci_execute(1个答案)
ORA-00933 SQL command not properly ended but good in SQL Developer(1个答案)
上个月关门了。
我使用flask和oracle作为我的后端,我试图获取数据,但它一直说无效字符,即使当我在Oracle中运行SQL时,SQL给予输出,但当在Flask中时,它是错误的无效字符

cur = con.cursor()
    user_id = session["user_id"]  #Type int
    cur.execute(
        """
        SELECT DISTINCT p.*,
        (CASE 
            WHEN user_id = :1 THEN 'joined'
            ELSE 'not'
        END) AS Result,
        (CASE 
            WHEN pub.request_to_join IS NULL THEN 'Private'
            WHEN pub.request_to_join = '1' THEN 'Public (Request)'
            ELSE 'Public'
        END) AS STATUS
        FROM ipm_project p
        LEFT JOIN ipm_project_public pub ON pub.project_id = p.id
        LEFT JOIN ipm_project_private pri ON pri.project_id = p.id
        JOIN ipm_project_team_junction pt ON p.id = pt.project_id
        JOIN ipm_project_team i ON i.id = pt.project_team_id
        ORDER BY p.id ASC;
        
        """, (user_id,)
    )
    
    columns = [col[0] for col in cur.description]
    cur.rowfactory = lambda *args: dict(zip(columns, args))
    all_projects = cur.fetchall()  // GETTING INVALID CHARACTER ERROR

字符串
This image shows that SQL above I run inside Oracle and the output shows , The SQL was same like above
This is the error

ldfqzlk8

ldfqzlk81#

尝试删除SQL命令末尾的分号;。当使用sqlplus等命令行工具时,这是一个命令结束指示符,但在使用Java等语言的编译程序时,通常不需要,其中假定每个SQL执行指令都是一个命令。这有助于防止SQL注入漏洞。
根据documentation
SQL语句不应包含尾随分号(“;“)或正斜杠(“/”)。这将失败:

cursor.execute("select * from MyTable;")

字符串
这是正确的:

cursor.execute("select * from MyTable")


所以这样做:

cur.execute(
    "SELECT DISTINCT p.*,
    (CASE 
        WHEN user_id = :1 THEN 'joined'
        ELSE 'not'
    END) AS Result,
    (CASE 
        WHEN pub.request_to_join IS NULL THEN 'Private'
        WHEN pub.request_to_join = '1' THEN 'Public (Request)'
        ELSE 'Public'
    END) AS STATUS
    FROM ipm_project p
    LEFT JOIN ipm_project_public pub ON pub.project_id = p.id
    LEFT JOIN ipm_project_private pri ON pri.project_id = p.id
    JOIN ipm_project_team_junction pt ON p.id = pt.project_id
    JOIN ipm_project_team i ON i.id = pt.project_team_id
    ORDER BY p.id ASC", user_id)

相关问题