简体   繁体   中英

Error while importing file into DB2 from python script

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()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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