【问题标题】:Query Oracle Database by Python用 Python 查询 Oracle 数据库
【发布时间】:2017-07-17 17:31:59
【问题描述】:

我想使用 python 查询数据库

这是我的代码:

import cx_Oracle
import os

def GetDatabase(queryCommand, row):
    conn = cx_Oracle.connect('Bell/Bell#@nn.nnn.nnn.nn:1521/Bell')
    cur = conn.cursor()
    cur.execute(queryCommand)
    res=cur.fetchone()
    i = 0
    while (i < row):
        if cur.rowcount == row:
            print res
        res=cur.fetchone()
        i = i + 1
    cur.close()
    conn.close()

tup1=GetDatabase("select Name from  ContactPoint  where EmployeeID='1234'",2)

这是输出

('KANOK BKK',)

我只想要值(我不想要括号和逗号)

我尝试打印 tup1[0] 但它不起作用。

【问题讨论】:

  • 包含实际 IP 地址可能不是一个好主意。

标签: python oracle plsql automation robotframework


【解决方案1】:

您应该使用yield 而不是print,或者将res 放入list 并返回,因为print 只需将其写入sys.stdout。

for row in tup1:  
    print(row[0])

【讨论】:

  • 我尝试在 tup1: print(row[0]) 中使用 row 但它不起作用。 tup1 TypeError 中的行错误:“NoneType”对象不可迭代
  • 您是否将print res 更改为yield res? NoneType ==>GetDatabase return None ,这意味着您没有按照我的建议正确执行。请仔细阅读!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多