Connecting to IBM AS400 server for database operations hangs

后端 未结 4 1718
走了就别回头了
走了就别回头了 2020-12-10 05:08

I\'m trying to talk to an AS400 in Python. The goal is to use SQLAlchemy, but when I couldn\'t get that to work I stepped back to a more basic script using just ibm_db inste

4条回答
  •  醉话见心
    2020-12-10 05:20

    Here is an example to work with as400, sqlalchemy and pandas. This exammple take a bunch of csv files and insert with pandas/sqlalchemy. Only works for windows, on linux the i series odbc driver segfaults (Centos 7 and Debian 9 x68_64)

    Client is Windows 10.

    My as400 version is 7.3

    Python is 2.7.14

    installed with pip: pandas, pyodbc, imb_db_sa, sqlalchemy

    You need to install i access for windows from ftp://public.dhe.ibm.com/as400/products/clientaccess/win32/v7r1m0/servicepack/si66062/

    Aditionally the modifications by @JohnY on pyodbc.py C:\Python27\Lib\site-packages\sqlalchemy\dialects\ibm_db_sa\pyodbc.py Change line 99 to

        pyodbc_driver_name = "IBM i Access ODBC Driver"
    

    The odbc driver changed it's name.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import pandas as pd
    import numpy as np
    from sqlalchemy import create_engine
    import glob
    
    csvfiles=(glob.glob("c:/Users/nahum/Documents/OUT/*.csv"))
    df_csvfiles = pd.DataFrame(csvfiles)
    for index, row in df_csvfiles.iterrows():
        datastore2=pd.read_csv(str(row[0]), delimiter=',', header=[0],skipfooter=3)
        engine = create_engine('ibm_db_sa+pyodbc://DB2_USER:PASSWORD@IP_SERVER/*local')
        datastore2.to_sql('table', engine, schema='SCHEMA', chunksize=1000, if_exists='append', index=False)
    

    Hope it helps.

提交回复
热议问题