我得到一个错误,但我的代码正在工作。Python和PostgreSQL

2024-06-02 00:58:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我并没有在字符串格式化期间转换所有参数,但我的代码实际上正在工作。有人能告诉我我的代码有什么问题吗? 当我运行这个时,它返回错误,但是当我通过调用Query\u Workers()查看工作列表时,我选择的人似乎已经被成功删除。你知道吗

def Remove_Directors(id, name):

    conn = None
    try:
        # read the connection parameters
        params = config()
        # connect to the PostgreSQL server
        conn = psycopg2.connect(**params)
        cur = conn.cursor()
        # create table one by one
        #for command in commands:
        #    cur.execute(command)
        SQL = "DELETE FROM directors WHERE id = (%s);"
        #cur.execute("DELETE FROM directors WHERE id = (%s)", (id))
        id = (id, )
        cur.execute(SQL, id)
        # close communication with the PostgreSQL database server
        cur.close()
        # commit the changes
        conn.commit()
        print ("%s has been removed from Directors.") % (name)
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

def Modify():

    print "Choose Options"

    option = raw_input("Press A for adding a member, R for removing a member, or V for Viewing members: ")
    if option.upper() == "A":
        print "Adding a member."
        Director_or_EventManager = raw_input("Is the new member a director or an event manager?\nPress D for Director or E for Event Manager:")
        if Director_or_EventManager.upper() == "D":
            ID_Entered_Correctly = False
            while ID_Entered_Correctly == False:
                id = raw_input("Enter 10 digit ID: ")
                if len(id) == 10:
                    ID_Entered_Correctly = True
                else:
                    print "Invalid ID"
            name = raw_input("Enter Name: ")
            Add_Directors(id, name)
        if Director_or_EventManager.upper() == "E":
            ID_Entered_Correctly = False
            while ID_Entered_Correctly == False:
                id = raw_input("Enter 10 digit ID: ")
                if len(id) == 10:
                    ID_Entered_Correctly = True
                else:
                    print "Invalid ID"
            name = raw_input("Enter Name: ")
            Add_Event_Managerss(id, name)

    elif option.upper() == "R":
        print "Removing a member."
        Director_or_EventManager = raw_input("Is the member a director or an event manager?\nPress D for Director or E for Event Manager:")
        if Director_or_EventManager.upper() == "D":
            conn = None
            try:
                params = config()
                conn = psycopg2.connect(**params)
                cur = conn.cursor()
                cur.execute("SELECT id, name FROM directors ORDER BY name")
                directors = cur.fetchall()
                print ("\tNumber\tID\t\tName")
                ids = []
                names = []
                count = 1
                for director in directors:
                    print ("\t%s\t%s\t%s") % (count, director[0], director[1])
                    ids.append(director[0])
                    names.append(directors[1])
                    count += 1
                cur.close()
            except (Exception, psycopg2.DatabaseError) as error:
                print(error)
            finally:
                if conn is not None:
                    conn.close()
            count -= 1
            num_director = int(raw_input("Enter the number of director to remove: "))
            if num_director <= 0 or num_director > count:
                print "Invalid entry"
            else:
                id = ids[num_director - 1]
                name = names[ids.index(id)]
                print id
                print name
                Remove_Directors(id, name)

    elif option.upper() == "V":
        Query_Workers()
    else:
        print "Invalid option"

Tags: orthenameidforinputrawif
1条回答
网友
1楼 · 发布于 2024-06-02 00:58:47

这个错误似乎发生在查询之后,所以它不影响数据更改,只影响输出调试。你知道吗

您只需更改这些行:

print ("%s has been removed from Directors.") % (name)

print ("%s has been removed from Directors." % name)

还有:

print ("\t%s\t%s\t%s") % (count, director[0], director[1])

print ("\t%s\t%s\t%s" % (count, director[0], director[1]))

相关问题 更多 >