2017-08-27 44 views
1

我没有得到在字符串格式化过程中转换的所有参数,但我的代码实际上工作。有人能告诉我我的代码有什么问题吗? 当我运行这个时,它会返回错误,但是当我通过调用Query_Workers()来查看列表中的工作时,看起来我选择的人已成功移除。我得到一个错误,但我的代码正在工作。 Python和PostgreSQL

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

的错误似乎在查询之后发生,所以它不会影响数据的变化,仅通过输出的调试。

你只需要改变这些行:

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])) 
相关问题