Я пытаюсь вставить данные в базу данных оракула через панд to_sql
from sqlalchemy import create_engine, types
import cx_Oracle
import pandas as pd
oracle_connection_string = ('oracle+cx_oracle://{username}:{password}@'+
cx_Oracle.makedsn('{hostname}',
'{port}', service_name='{service_name}'))
engine = create_engine(oracle_connection_string.format(
username='test',
password ='test',
hostname='test.server.com',
port='1521',
service_name='test.server.net'
)) ### Default encoding is utf-8 as i am using python3
df = pd.DataFrame(["Create Buying Opportunity as Underlying ",
"strategic optimisation to drive more",
"Deliver Growth Without Sacrificing Margins",
"while expanding operating margins"], columns=["column1"])
df.column1 = df.column1.str.encode('utf8')
###This is required because I have some non ascii characters in my text
####Below is the output of the dataframe - df
column1
0 b'Create Buying Opportunity as Underlying '
1 b'strategic optimisation to drive more'
2 b'Deliver Growth Without Sacrificing Margins'
3 b'while expanding operating margins'
Если я непосредственно отправляю данные в oracle DB, они работают без каких-либо проблем, но единственная проблема заключается в том, что для вставки записей 145 Кб всегда требуется код, указанный ниже
df.to_sql(name='TEST_TABLE', con=engine, if_exists='append', index=False)
Поскольку приведенная выше команда занимает больше времени, в соответствии с предложениями других пользователей из github, я внес изменения, как показано ниже
dtyp = {c:types.VARCHAR(df[c].str.len().max())
for c in df.columns[df.dtypes == 'object'].tolist()}
df.to_sql(name='TEST_TABLE', con=engine, if_exists='append', index=False, dtype=dtyp)
Теперь данные вставляются в таблицу как «415041432053656D69636F6E647563746F7» вместо текста «Создать возможность покупки как базовую»