将IBM

编程入门 行业动态 更新时间:2024-10-12 03:16:49
本文介绍了将IBM_DB与Pandas一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Python语言中的数据分析工具Pandas.我正在尝试使用 ibm_db 包从IBM DB读取数据.根据Pandas网站上的文档,我们需要至少提供2个参数,一个是将要执行的sql,另一个是数据库的连接对象.但是当我这样做时,它给我一个错误,即连接对象中没有cursor()方法.我想也许这不是特定的数据库包的工作方式.我试图找到一些解决方法,但没有成功.

代码:

print "hello PyDev" con = db.connect("DATABASE=db;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=admin;PWD=admin;", "", "") sql = "select * from Maximo.PLUSPCUSTOMER" stmt = db.exec_immediate(con,sql) pd.read_sql(sql, db) print "done here"

错误:

hello PyDev Traceback (most recent call last): File "C:\Users\ray\workspace\Firstproject\pack\test.py", line 15, in <module> pd.read_sql(sql, con) File "D:\etl\lib\site-packages\pandas\io\sql.py", line 478, in read_sql chunksize=chunksize) File "D:\etl\lib\site-packages\pandas\io\sql.py", line 1504, in read_query cursor = self.execute(*args) File "D:\etl\lib\site-packages\pandas\io\sql.py", line 1467, in execute cur = self.con.cursor() AttributeError: 'ibm_db.IBM_DBConnection' object has no attribute 'cursor'

如果我能够从数据库中获取数据,则可以获取数据,但是我需要读入数据框并在处理数据后需要写回数据库.

从数据库中获取代码

stmt = db.exec_immediate(con,sql) tpl=db.fetch_tuple(stmt) while tpl: print(tpl) tpl=db.fetch_tuple(stmt)

解决方案

在进一步研究软件包时,我发现需要将IBM_DB连接对象包装在ibm_db_dbi连接对象中,该对象是同一软件包的一部分. /p>

所以

conn = ibm_db_dbi.Connection(con) df = pd.read_sql(sql, conn)

上面的代码有效,熊​​猫成功地将数据提取到数据帧中.

I am trying to use the data analysis tool Pandas in Python Language. I am trying to read data from a IBM DB, using ibm_db package. According to the documentation in Pandas website we need to provide at least 2 arguments, one would be the sql that would be executed and other would be the connection object of the database. But when i do that, it gives me error that the connection object does not have a cursor() method in it. I figured maybe this is not how this particular DB Package worked. I tried to find a few workarounds but was not successfull.

Code:

print "hello PyDev" con = db.connect("DATABASE=db;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=admin;PWD=admin;", "", "") sql = "select * from Maximo.PLUSPCUSTOMER" stmt = db.exec_immediate(con,sql) pd.read_sql(sql, db) print "done here"

Error:

hello PyDev Traceback (most recent call last): File "C:\Users\ray\workspace\Firstproject\pack\test.py", line 15, in <module> pd.read_sql(sql, con) File "D:\etl\lib\site-packages\pandas\io\sql.py", line 478, in read_sql chunksize=chunksize) File "D:\etl\lib\site-packages\pandas\io\sql.py", line 1504, in read_query cursor = self.execute(*args) File "D:\etl\lib\site-packages\pandas\io\sql.py", line 1467, in execute cur = self.con.cursor() AttributeError: 'ibm_db.IBM_DBConnection' object has no attribute 'cursor'

I am able to fetch data if i fetch it from the database but i need to read into a dataframe and need to write back to the database after processing data.

Code for fetching from DB

stmt = db.exec_immediate(con,sql) tpl=db.fetch_tuple(stmt) while tpl: print(tpl) tpl=db.fetch_tuple(stmt)

解决方案

On doing further studying the package, i found that I need to wrap the IBM_DB connection object in a ibm_db_dbi connection object, which is part of the same package.

So

conn = ibm_db_dbi.Connection(con) df = pd.read_sql(sql, conn)

The above code works and pandas fetches data into dataframe successfully.

更多推荐

将IBM

本文发布于:2023-11-24 11:06:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IBM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!