Python Forum
connect sql by python using txt. file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
connect sql by python using txt. file
#1
Hi python experts i do not know what i do wrong , I would like connect to sql by python using txt.file , Where i do a mistake? or is possible connect to sql by python so as not to be seen password and other things? Thanks Password and other thing should be saved in other folder as my python file. Is it possible thanks

dat=open(location+"udaje.txt",'r')
lines=dat.readlines()
p_username=lines[0]
p_password=lines[1]
p_host=lines[2]
p_service=lines[3]
p_dns=lines[4]
p_port=lines[5]

print(p_username)
print(p_password)
print(p_host)
print(p_service)
print(p_dns)
print(p_port)

#dat.close()

conn=cx_Oracle.connect(user=p_username, password=p_password, dsn=p_dns)
cursor = conn.cursor()
rs = cursor.execute("delete FROM table")
conn.commit()
I get this output

Output:
DatabaseError Traceback (most recent call last) <ipython-input-11-544a4434c1b5> in <module> 37 #dat.close() 38 ---> 39 conn=cx_Oracle.connect(user=p_username, password=p_password, dsn=p_dns) 40 cursor = conn.cursor() 41 rs = cursor.execute("delete FROM table") DatabaseError: ORA-12154: TNS: nie je možné rozpoznať zadaný identifikátor pripojenia
Reply
#2
Hi,

instead of using a text file, use another Python file and define the values for username etc. in there. Then import from there. Everything which is in object - which variables are - is importable. Or use an ini-style file and use the configparser module to read from it. And in case you like to stick to a text file, do not forget to provide the encoding to the open function.
When reading a file, it is better to use the with-statement. This ensures the file is closed properly again.

And please to not build filepathes with +, use pathlib (which is included in Python) instead. That's the clean way.

On your code: syntactically the line is correct I guess - are you sure the values of the parameters you pass are _really_ valid? The error code ORA-12154 indicates that either a parameter is wrong or something is wrong in the tnsnames.ora file.

Regards, noisefloor
Reply
#3
Let's say your udaje.txt looks like this:
Output:
username password host service dns port
I write a program to print out the file.
with open("udaje.txt", "r") as file:
    print(*file.readlines(), sep="\n")
This is the output:
username

password

host

service

dns

port
That may be your problem. Each of the strings in the file has a following newline character, and these will be at the end of your username and password strings. Your username likely doesn't end with "\n".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Connect to PostgreSQL Through Jump Server and SSH Tunnel using Python? nishans 1 1,016 Jan-02-2024, 10:37 AM
Last Post: khanzain
  python connect to mssql wailoonho 7 1,613 Dec-07-2023, 02:06 AM
Last Post: wailoonho
  Using Python to connect to an XML ? jehoshua 12 1,994 Jul-11-2023, 12:34 AM
Last Post: jehoshua
  Trying to make a bot to connect on discord with Selenium Python johnsmith43 2 52,004 Mar-21-2022, 02:56 PM
Last Post: Cloudytechnical
  How to connect Mysql databse with python and VSCode IDE madhusoodhananER 1 8,738 Oct-31-2019, 10:15 AM
Last Post: Larz60+
  Connect a Teradata DB to Python OscarBoots 10 8,887 Jan-31-2019, 10:23 PM
Last Post: OscarBoots
  Python connect to Sybase IQ FORTITUDE 1 4,794 Jan-24-2019, 02:14 AM
Last Post: micseydel
  How to connect Atom and Python? Jack_Sparrow 1 3,758 May-01-2018, 10:53 AM
Last Post: Larz60+
  connect to remote database via python script sunstar20 5 5,365 Apr-23-2018, 11:05 AM
Last Post: Gribouillis
  Can anyone Please help on fixing this python code to connect and save things on sqlit Roscoes 2 2,918 Mar-06-2018, 04:48 AM
Last Post: Roscoes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020