简体   繁体   English

将 cx_Oracle 用于 Python 并接收 TypeError: 'NoneType' object 在将数据插入表时不可迭代

[英]Using cx_Oracle for Python and receiving TypeError: 'NoneType' object is not iterable when inserting data to table

I am trying to insert data into a Oracle database.我正在尝试将数据插入 Oracle 数据库。 I have to use a proxy to have insert permissions.我必须使用代理才能获得插入权限。 Below is the code that I run, but I'm getting the error 'TypeError: 'NoneType' object is not iterable' at the line ''',conn).下面是我运行的代码,但在 ''',conn 行出现错误'TypeError: 'NoneType' object is not iterable'。

I'm not sure where to go from here.我不确定 go 从这里到哪里。 Does anyone know what I'm doing wrong?有谁知道我做错了什么? Full error message is below the query code.完整的错误消息在查询代码下方。

dsn_tns = cx_Oracle.makedsn('hostname', 'port', service_name='service_name') 
conn = cx_Oracle.connect(user="username[proxy]", password=r'password', dsn=dsn_tns)

sqlQuery = pd.read_sql_query(
'''insert into intl.persons1(first_name, last_name) values ('sunshines','heffleflopper')
''', conn) 

df = pd.DataFrame(sqlQuery, columns = ['PERSON_ID','FIRST_NAME','LAST_NAME'])
print(df)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-454fb5b824e6> in <module>()
      4 sqlQuery = pd.read_sql_query(
      5 '''insert into intl.persons1(first_name, last_name) values ('sunshines','heffleflopper')
----> 6 ''', conn) 
      7 
      8 dfPOFullHistory = pd.DataFrame(sqlQuery, columns = ['PERSON_ID','FIRST_NAME','LAST_NAME'])

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in read_sql_query(sql, con, index_col, coerce_float, params, parse_dates, chunksize)
    312     return pandas_sql.read_query(
    313         sql, index_col=index_col, params=params, coerce_float=coerce_float,
--> 314         parse_dates=parse_dates, chunksize=chunksize)
    315 
    316 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
   1434         args = _convert_params(sql, params)
   1435         cursor = self.execute(*args)
-> 1436         columns = [col_desc[0] for col_desc in cursor.description]
   1437 
   1438         if chunksize is not None:

TypeError: 'NoneType' object is not iterable

An INSERT statement does not return a result set. INSERT语句不返回结果集。

You are using pandas.read_sql_query which:您正在使用pandas.read_sql_query其中:

Returns a DataFrame corresponding to the result set of the query string.返回查询字符串的结果集对应的 DataFrame。

However, since your query is not returning a result set then this is the reason the error is generated;但是,由于您的查询没有返回结果集,因此这就是生成错误的原因; you are using the wrong method to insert values.您使用错误的方法插入值。

You appear to need to insert using cx_oracle rather than trying to do it through pandas.您似乎需要使用cx_oracle插入,而不是尝试通过 pandas 插入。

It shows you exact line: columns = [col_desc[0] for col_desc in cursor.description] .它向您显示确切的行: columns = [col_desc[0] for col_desc in cursor.description] The only iterable here is cursor.description .这里唯一可迭代的是cursor.description If you check the documentation page , you'll find:如果您查看文档页面,您会发现:

This attribute will be None for operations that do not return rows or if the cursor has not had an operation invoked via the execute() method yet.对于不返回行的操作或 cursor 尚未通过 execute() 方法调用的操作,此属性将为 None。

INSERT does not return rows. INSERT不返回行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM