How to store Pandas DataFrame with Int64 to MS Access table

43 views Asked by At

I'm on python 3.11.8, sqlalchemy 2.0.25, sqlalchemy-access 2.0.2, pyodbc 5.0.1, pandas 2.2.1, MS Access 2016.

Connection looks like:

engine = sa.create_engine("access+pyodbc://@my_accdb")
conn = engine.connect()

Here's part of my code:

    tab20 = pd.read_sql_table("table_name", conn)[['col1', 'col2']]
    print(tab20.head())
    df = tab20.dropna()
    print(df.head())
    print(df.dtypes)
    df = df.astype('int64') # this runs okay
    df_dtypes = {
        "col1": sa.types.BigInteger,
        "col2": sa_a.LongInteger
    }
    print(df.dtypes)

    df.to_sql("new_table", conn, index=False, if_exists='replace', dtype=df_dtypes)

    conn.commit()

col1 contains value of 11-digit numbers, col2 contains value of 5-digit numbers.
Originally in MS Access, col1 values are "Large Number", col2 values are "Number"(field size: long integer)
I would like to keep their data type as in MS Access.
Currently, I'm able to convert column dtype to 'Int64'. When I try to write to ms access table, it got error:

sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('HYC00', '\[HYC00\] \[Microsoft\]\[ODBC Microsoft Access Driver\]Optional feature not implemented  (106) (SQLBindParameter)')
\[SQL: INSERT INTO \[new_table\] (\[Col1\], \[Col2\]) VALUES (?, ?)\]
\[parameters: \[(72001956300, 601), (72001956400, 601), (72001956500, 601), (72001956600, 601), (72001956700, 601), (72001956800, 601), (72141957600, 601),
(72003430100, 602)  ... displaying 10 of 168212 total bound parameter sets ...  (2198000100, 99927), (2275000300, 99929)\]\]
(Background on this error at: https://sqlalche.me/e/20/dbapi)

It works with convert to 'float64', but I would like to make them integers.
I've also tried directly using pyodbc, but receives same error.
This happens to all situations when I try to write int64 to sql table.
How can I fix this?

UPDATES: I'm able to run without error by removing astype() line, but this leads me to more error (UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 0-1: illegal UTF-16 surrogate) in other functions.

UPDATES 1:
Now I doubt about my source data. Getting unicode decoding error even I went with to_csv. Might be the converting problem? (I don't like Access, try to persuade my boss not using it

0

There are 0 answers