Reputation: 35
I am trying to connect to oracle from python using cx_Oracle. MY login and password on known I don't know what use for a dsn.
I am using tkinter to create a CRUD operations to a oracle db. My function is written to read data from the db. My cx_Oracle.connect
fails because I am not passing the right parameters.
I used @localhost:1521:xe
for the dsn, but it gave a SyntaxError: positional argument follows keyword argument.
Can you please help me key in the right parameters to connect? I am trying to connect to Oracle 10g express edition.
# -*- coding: utf-8 -*-
#Created on Fri Jul 12 15:50:54 2019
#@author: CGDELL23
import tkinter as tk
import cx_Oracle
class Application(tk.Frame):
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.grid()
self.createWidgets()
def createWidgets(self):
#Employee Details
self.eNameLabel = tk.Label(self,text='Employee Name')
self.eNameValue = tk.Entry(self)
self.eNameLabel.grid(row=0,column=0)
self.eNameValue.grid(row=0,column=1)
self.eIdLabel = tk.Label(self, text='Employee Id')
self.eIdValue = tk.Entry(self)
self.eIdLabel.grid(row=1,column=0)
self.eIdValue.grid(row=1,column=1)
self.eSalaryLabel = tk.Label(self, text='Employee Salary')
self.eSalaryValue = tk.Entry(self)
self.eSalaryLabel.grid(row=2,column=0)
self.eSalaryValue.grid(row=2,column=1)
#CRUD Buttons
self.CreateButton = tk.Button(self,text='Create', command=self.Create)
self.CreateButton.grid(row=3,column=0)
self.ReadButton = tk.Button(self,text='Read', command=self.Read)
self.ReadButton.grid(row=3,column=1)
self.UpdateButton = tk.Button(self,text='Update', command=self.Update)
self.UpdateButton.grid(row=3,column=2)
self.DeleteButton = tk.Button(self,text='Delete', command=self.Delete)
self.DeleteButton.grid(row=3,column=3)
self.ExitButton = tk.Button(self,text='Exit', command=self.Exited)
self.ExitButton.grid(row=3,column=4)
#List the CRUD operations functions
def Create(self):
print ('Create Button Pressed')
#def Read(self):
# print ('Read Button Pressed')
def Update(self):
print ('Update Button Pressed')
def Delete(self):
print ('Delete Button Pressed')
def Exited(self):
print ('Exit Button Pressed')
exit(0)
class odbc:
dsn_tns=cx_Oracle.makedsn('localhost','8080',service_name='OracleODBC')
conn=cx_Oracle.connect(user='wm910',password='wm910','@localhost:1521:xe')
def Read(self):
c=conn.cursor()
c.execute('select * from Employee where empId = 51')
for row in c:
print (row[0], '-', row[1])
conn.close()
c = Application()
c.master.title('Sample Application')
c.mainloop()
Upvotes: 2
Views: 6288
Reputation: 8262
It looks like you've correctly called the makedsn()
function but you never used the value returned when calling connect()
.
According to the cx_Oracle documentation for connect()
, the dsn=
parameter can be the following:
The dsn (data source name) is the TNS entry (from the Oracle names server or tnsnames.ora file) or is a string like the one returned from makedsn().
Try changing the your call to connect()
to the following:
conn = cx_Oracle.connect(user='wm910', password='wm910', dsn=dsn_tns)
EDIT: Or, as suggested by @ChristopherJones in his comment, replace the dsn_tns
variable above with "localhost:1521/xe"
.
Alternatively, you can use a single string as an argument to connect()
as described further down:
If only one parameter is passed, a connect string is assumed which is to be of the format user/password@dsn, the same format accepted by Oracle applications such as SQL*Plus.
Elsewhere in the docs, the sample DSN string provided is like so:
connection = cx_Oracle.connect("hr", "welcome", "localhost/orclpdb")
You can remove user=
and password=
and use only positional arguments like they did, passing in a valid string for the DSN in that form:
connection = cx_Oracle.connect("wm910", "wm910", "localhost/OracleODBC")
Assuming that the hostname/service_name
is localhost/OracleODBC
.
Upvotes: 1