【问题标题】:How to error check a function containing a SQL query with the placeholder %s如何错误检查包含带有占位符 %s 的 SQL 查询的函数
【发布时间】:2019-04-08 00:34:42
【问题描述】:

我正在为学校做一个项目,但遇到了一个十字路口。我在 python 脚本中有一堆查询,用户可以在运行脚本时搜索这些查询。其中一些函数使用 %s 作为占位符,以便用户可以输入值。

但是,我想检查他们输入的内容是否确实在我的数据库中。例如,如果您向用户询问电影类型,如果他们输入的内容不在我的表格中,则应该会产生错误。

我花了几天时间试图找到一种方法来做到这一点,但没有运气。我不是最擅长函数的,有时我会把事情搞混。

为了简化事情,我复制了一个需要用户输入进行测试的查询。我以为我已经想出了一个关于如何进行错误检查的解决方案,但没有运气。我已经粘贴了下面的代码。如果您知道我缺少什么,我们将非常感谢您的帮助。

#!/usr/bin/python36

#Modules
############################
import psycopg2
import sys
import os

#Open Database
############################
global cursor

#def OpenDatabase():
try:
        connection = psycopg2.connect(database='Blockbuster36', user='dbadmin')
        cursor = connection.cursor()
except psycopg2.DatabaseError:
        print("No connection to database.")
        sys.exit(1)

#Functions
###########################
def Show_Tuples():
        tuple = cursor.fetchone()
        while tuple != None:
                print(tuple)
                tuple = cursor.fetchone()

#3) Display all films of a certain genre
def Qry3_Film_of_Genre(Genre_Choice):
        Qry3 = '''select Film_Title, Genre_Name from Film 
                join Film_Genre 
                on Film.Film_ID = Film_Genre.Film_ID 
                join Genre 
                on Film_Genre.Genre_ID = Genre.Genre_ID 
                where Genre_Name = %s;'''

        cursor.execute(Qry3, (Genre_Choice,))
        Show_Tuples()

def Qry3_Error_Check(Genre_Choice):
        try:
                Qry3_ec = "select Genre_ID, Genre_Name from Genre where Genre_Name = %s;"
                cursor.execute(Qry3_ec, (Genre_Choice,))
                results = cursor.fetchone()
                if results[0] > 0:
                        print()
                        Qry3_Film_of_Genre(Genre_Choice)
#               else:
                elif results[0] == None:
                        print("This Genre does not exist")
                        Genre_Choice = input("Please enter the Genre name to filter movies by: ")
        except psycopg2.Error as query_issue:
                print("Something wrong with the query", query_issue)
        except Exception as unkown:
                print("Error: Something else went wrong")
                print("Here is the issue: ", unkown)

#Main Code
##########################
Genre_Choice = input("Please enter the Genre name to filter movies by: ")
Check_Genre = Qry3_Error_Check(Genre_Choice)
#print("Function Return: ", Check_Genre)
#print()

#print(Check_Genre)
#if Check_Genre == None:
#       print("This Genre does not exist")
#       Genre_Choice = input("Please enter the Genre name to filter movies by: ")
#       Check_Genre = Qry3_Error_Check(Genre_Choice)
#elif Check_Genre != None:
#       Qry3_Film_of_Genre(Genre_Choice)

#while Check_Genre != Genre_Choice:
#       print("This Genre does not exist")
#       Genre_Choice = input("Please enter the Genre name to filter movies by: ")
#       Check_Genre = Qry3_Error_Check(Genre_Choice)
#if Check_Genre == None:
#       Qry3_Film_of_Genre(Genre_Choice)

#Close Cursor and Database
###################################
cursor.close()
connection.close()

基本上,我希望在输入另一个流派名称的同时继续打印错误消息,直到输入有效的流派。现在,即使输入有效的流派名称并使用 Qry3_Error_Check 函数获取输出,它也会一直说是错误的。

一旦用户输入了有效的流派名称,根据错误检查功能查询,就会出现原始查询。


我已经取得了一些进展,现在可以输入有效的流派了。但是,当输入无效的流派名称时,它会跳转到一般例外并打印错误“NoneType object is not subscriptable”。显然,没有匹配的行,因此出现 NoneType 错误。但是,它应该在上面的 elif 语句中重新提示用户。输入什么作为 elif 语句以便重新提示用户输入有效的流派名称? 注意:我现在注释掉了我的大部分主要代码。

【问题讨论】:

    标签: python-3.6 psycopg2 postgresql-9.2


    【解决方案1】:

    如果您想专门检查 SQL 错误,您可以为它们设置一个单独的 except 块。此外,对控制流使用异常通常(但并非总是)是个坏主意。

    类似下面的东西(我没有测试过)可能接近你所需要的。

    try:
        Qry3_ec = "select Genre_ID, Genre_Name from Genre where Genre_Name = %s;"
        cursor.execute(Qry3_ec, (Genre_Choice,))
        results = cursor.fetchall()
        if results.count > 0:
            print(results)
        else:
            print("Nothing found!")
    except psycopg2.Error as e:
        print("Something wrong with the query")
        print(e)
    except Exception as e
        print("Something else went wrong")
        print(e)
    

    祝你的项目好运。 :)

    【讨论】:

    • 不,这不起作用,即使您输入了有效的流派名称,也只会打印“其他错误”。此外,"as e" 产生了一般的语法错误,除了
    • 为第二个异常块尝试“except Exception as e”。
    • 哎呀,忘了在 except 后面加上 Exception 这个词,谢谢 Devasta。它现在不喜欢 if 语句。运行脚本时出现一个奇怪的错误 = ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ 请输入流派名称来过滤电影:动作 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 别的去了'builtin_function_or_method' 和 'int' 的实例之间不支持错误的 '>'
    猜你喜欢
    • 2022-01-24
    • 2020-08-27
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2022-07-22
    相关资源
    最近更新 更多