首页 > 解决方案 > 你在连接字符串中使用什么dsn来使用python连接到oracle

问题描述

我正在尝试使用 cx_Oracle 从 python 连接到 oracle。我的登录名和密码知道我不知道 dsn 有什么用。

我正在使用 tkinter 创建对 oracle 数据库的 CRUD 操作。我的函数是为了从数据库中读取数据而编写的。我cx_Oracle.connect失败了,因为我没有传递正确的参数。

我使用@localhost:1521:xe了 dsn,但它给出了 SyntaxError: positional argument following keyword argument。

你能帮我输入正确的连接参数吗?我正在尝试连接到 Oracle 10g 速成版。

# -*- 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()

标签: pythondatabaseconnectivity

解决方案


看起来您已经正确调用了该makedsn()函数,但您从未使用过调用时返回的值connect()

根据cx_Oracle 文档connect()dsn=参数可以如下:

dsn(数据源名称)是 TNS 条目(来自 Oracle 名称服务器或 tnsnames.ora 文件)或类似于从 makedsn() 返回的字符串。

尝试将您的呼叫更改connect()为以下内容:

conn = cx_Oracle.connect(user='wm910', password='wm910', dsn=dsn_tns)

编辑:或者,正如@ChristopherJones 在他的评论中所建议的,将dsn_tns上面的变量替换为"localhost:1521/xe".

或者,您可以使用单个字符串作为参数,connect()如下所述:

如果只传递一个参数,则假定连接字符串的格式为user/password@dsn,与 Oracle 应用程序(如 SQL*Plus)接受的格式相同。

在文档的其他地方,提供的示例 DSN 字符串如下所示:

connection = cx_Oracle.connect("hr", "welcome", "localhost/orclpdb")

您可以像他们一样删除user=和仅使用位置参数,以该形式为 DSN 传递一个有效字符串:password=

connection = cx_Oracle.connect("wm910", "wm910", "localhost/OracleODBC")

假设hostname/service_namelocalhost/OracleODBC


推荐阅读