Using cx_Oracle for Python and receiving TypeError: 'NoneType' object is not iterable when inserting data to table
Using cx_Oracle for Python and receiving TypeError: 'NoneType' object is not iterable when inserting data to table
我正在尝试将数据插入 Oracle 数据库。我必须使用代理才能获得插入权限。下面是我 运行 的代码,但我在 ''',conn).
行收到错误 'TypeError: 'NoneType' object is not iterable'
我不确定从这里到哪里去。有谁知道我做错了什么?完整的错误消息在查询代码下方。
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
INSERT
语句没有 return 结果集。
您正在使用 pandas.read_sql_query
其中:
Returns a DataFrame corresponding to the result set of the query string.
但是,由于您的查询不是 return 结果集,因此这就是产生错误的原因;您使用了错误的方法来插入值。
您似乎需要 insert using cx_oracle
而不是尝试通过 pandas 来完成。
它向您显示确切的行:columns = [col_desc[0] for col_desc in cursor.description]
。这里唯一可迭代的是 cursor.description
。如果您检查 the documentation page,您会发现:
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.
INSERT
没有 return 行。
我正在尝试将数据插入 Oracle 数据库。我必须使用代理才能获得插入权限。下面是我 运行 的代码,但我在 ''',conn).
行收到错误 'TypeError: 'NoneType' object is not iterable'我不确定从这里到哪里去。有谁知道我做错了什么?完整的错误消息在查询代码下方。
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
INSERT
语句没有 return 结果集。
您正在使用 pandas.read_sql_query
其中:
Returns a DataFrame corresponding to the result set of the query string.
但是,由于您的查询不是 return 结果集,因此这就是产生错误的原因;您使用了错误的方法来插入值。
您似乎需要 insert using cx_oracle
而不是尝试通过 pandas 来完成。
它向您显示确切的行:columns = [col_desc[0] for col_desc in cursor.description]
。这里唯一可迭代的是 cursor.description
。如果您检查 the documentation page,您会发现:
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.
INSERT
没有 return 行。