Error while importing file into DB2 from python script

微笑、不失礼 提交于 2019-12-11 12:42:19

问题


Getting the below error while trying to import a ^ delimited file into a DB2 database using python 2.4.3.

Error:

Traceback (most recent call last):
  File "C:\Python25\Usefulscripts\order.py", line 89, in <module>
    load_order_stack() 
  File "C:\Python25\Usefulscripts\order.py", line 75, in load_order_stack
    conn2.execute(importTmp)
ProgrammingError: ('42601', '[42601] [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N  An unexpected token "orders_extract"

was found following "import from ".

Code:

import pyodbc

def load_order_stack():
    try:
        conn2 = pyodbc.connect('DSN=db2Database;UID=ueserid;PWD=password')
        importTmp = ("import from orders_extract of del modified by coldel0x5E"
                     "insert_update into test.ORDERS_Table (ORDER_ID,item,price);")
        conn2.execute(importTmp)
        conn2.commit()

回答1:


IMPORT is not an SQL statement. It is a DB2 Command Line Processor (CLP) command and as such can only be run by the said CLP.

There is an SQL interface to some CLP commands via calls to the ADMIN_CMD() stored procedure, please check the manual: IMPORT using ADMIN_CMD




回答2:


You also have the option of reading the file, line by line, and inserting into your database. This will definitely be slower than any native import operation. Assuming your delimited file structure is, and the file is named input.txt:

ORDER_ID^item^price
1^'bat'^50.00
2^'ball'^25.00

Code:

import csv
import pyodbc

connection = pyodbc.connect('DSN=db2Database;UID=ueserid;PWD=password')
cursor = connection.cursor()

with open('input.txt', 'rb') as f:
    rows = csv.reader(f, delimiter='^')
    # get column names from header in first line
    columns = ','.join(next(rows))
    for row in rows:
        # build sql with placeholders for insert
        placeholders = ','.join('?' * len(row))
        sql = 'insert into ({}) values ({});'.format(columns, placeholders)

        # execute parameterized database insert
        cursor.execute(sql, row)
        cursor.commit()

Play around with commit() placement, you probably want to commit in batches to improve performance.



来源:https://stackoverflow.com/questions/16562381/error-while-importing-file-into-db2-from-python-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!