Currently this is how I am creating a SQL connection string.
def connect_DB():
admin_connection_info = {
"user" : "USER" ,
"password" :"qihvnw@zwA@8" ,
"service" : "abcdmcewbv.com",
"port" :"10000" ,
"host" : "localhost"
}
admin_connection_string = '{user}/{password}@{host}:{port}/{service}'.format(**admin_connection_info) # constructing connection string
print("DB Connection String, ADMIN " + str(admin_connection_string))
return admin_connection_string
I face issues when my password contains special characters like @ at any position
Case 1 Details:
username: USER
pwd: qihvnw@zwA@8
host: localhost
port: 10000
ServiceName: abcdmcewbv.com
'@' issue connection string shown below:
USER/qihvnw@zwA@8@localhost:10000/abcdmcewbv.com
once it encounters the first '@', it assumes everything after that till the first ':' is host name. here it probably assumes,
pwd: qihvnw
host: zwA@8@localhost
This is wrong.
Case 2 Details:
username: USER//
pwd: qihvnw@zwA@8
host: localhost
port: 10000
ServiceName: abcdmcewbv.com
'//' issue in username connection string below
USER///qihvnw@zwA@8@localhost:10000/abcdmcewbv.com
Here even though username is USER//
It assumes everything after the first '/' to be part of the password, i.e
correct password -> qihvnw@zwA@8
wrong password -> //qihvnw (Case 1 issue carries over here)
How do I resolve such a case. This is only a sample of what the password could look like and these are from ADMIN, I cannot ask them to not use such special characters.
Please suggest a workaround to build a valid connection string. The above mentioned parameters are compulsory (uname,pwd,host,port,servicename)
If there is an alternate more robust method of building connection strings that can handle such edge cases please do let me know