使用提供错误的Jaydebeapi将Pandas DataFrame写入Teradata数据库表

时间:2019-05-14 11:40:37

标签: python teradata jaydebeapi

我正在尝试使用Jaydebeapi将熊猫数据框写入Teradata数据库。我能够轻松地从数据库读取数据,但是在编写时遇到问题。

代码:

import jaydebeapi
import pandas as pd
case_detail_server ='server_url'
server_name= 'servername'
user ='xyz'
password ='****'
jars= ['/anaconda3/lib/tdgssconfig.jar','/anaconda3/lib/terajdbc4.jar']
#jars= ['/anaconda3/lib/terajdbc4.jar']
jclassname_case_detail='com.teradata.jdbc.TeraDriver'
teradata_url='jdbc:teradata://server_url/DBS_PORT=1025,TMODE=ANSI,CHARSET=UTF8'
conn = jaydebeapi.connect('com.teradata.jdbc.TeraDriver',teradata_url,{'user': user, 'password': password},jars)
curs = conn.cursor()

方法1: 要写的熊猫表=> df_out

for row in df_out.head().iterrows():
    curs.execute("INSERT INTO TD_table(column1, column2) VALUES(?,?)", row)

错误1

Error
---------------------------------------------------------------------------
Error
                                    Traceback (most recent call last)
<ipython-input-390-8067f51cb5e3> in <module>
      1 for row in df_out.head().iterrows():
----> 2     curs.execute("INSERT INTO CTD_table(column1, column2) VALUES(?,?)", row)
/anaconda3/lib/python3.6/site-packages/jaydebeapi/__init__.py in execute(self, operation, parameters)
    492     def execute(self, operation, parameters=None):
    493         if self._connection._closed:
--> 494             raise Error()
    495         if not parameters:
    496             parameters = ()
Error: 

方法2:

resultset=[]
for column1,column2 in df_out:
    try:
        resultset.append((column1,column2))
    except AttributeError:
        pass
curs.executemany("INSERT INTO CTD_table(column1, column2) VALUES (?,?)",resultset)

错误2

ValueError: too many values to unpack (expected 2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-392-71473191f932> in <module>
      1 resultset=[]
----> 2 for column1,column2 in df_out:
      3     try:
      4         resultset.append((column1,column2))
      5     except AttributeError:
ValueError: too many values to unpack (expected 2)```

0 个答案:

没有答案