我收到错误,但我的代码正在运行。 Python和PostgreSQL

时间:2017-08-27 19:58:40

标签: python postgresql

我没有在字符串格式化过程中转换所有参数,但我的代码实际上正在运行。有人能告诉我我的代码有什么问题吗? 当我运行它时,它会返回错误,但当我通过调用Query_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"

1 个答案:

答案 0 :(得分:1)

错误似乎发生在查询之后,因此它不会影响数据更改,只会影响输出调试。

您只需要更改这些行:

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]))