Dec-05-2018, 04:12 PM
Hello,
I'm writing a program that will list available databases to a user based on their tnsnames.ora and then allow them to make a connection to one available. I want to return a message if they attempt to use something other than what's available in the list.
Here is a snippet of the code:
Do I need to add another exception or would it be something of an if/else statement so they can continue to login once the correct database is entered?
Thanks!
I'm writing a program that will list available databases to a user based on their tnsnames.ora and then allow them to make a connection to one available. I want to return a message if they attempt to use something other than what's available in the list.
Here is a snippet of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
def db_menu(): os.chdir(r 'c:\temp' ) # move to tns directoy; raw string tns = open ( 'tnsnames.ora' , 'r' ) # open file in read mode sList = tns.read() # read file into string sList = re.sub( "#.*\n" , "", sList) # strip all comments sList = re.sub( "\(.*\n" , " ", sList) # remove " (" to end of line sList = re.sub( "\)" , " ", sList) # clean up " )" sList = re.sub( " " , "", sList) # remove spaces sList = re.sub( "\n" , "", sList) # remove carriage returns sList = re.sub( "=$" , " ", sList) # remove last " = " sList = sList.split( "=" ) # create list tns.close() print ( '\nAvailable Databases: ' ) for i in sList: print (i) # Database Login def db_login(): db_menu() while True : try : # Input username and password uname = input ( '\nEnter username: ' ) pw = getpass.getpass(prompt = 'Enter password: ' ) db = input ( 'Enter database: ' ) conn_str = u '%s/%s@%s' % (uname, pw, db) # connection string conn = cx_Oracle.connect(conn_str) # connection to database cur = conn.cursor() # parameter to access cursor method q_str = "select * from global_name" # create a query string cur.execute(q_str) # pass to cursor method for row in cur: print ( "\nConnected to: {}" . format (row)) else : break except cx_Oracle.DatabaseError as e: error, = e.args print ( "\nInvalid username/password. Please try again.\n" ) |
Thanks!