Generation

generate functionMon, 06 Feb 2023

using sqlalchemy, transfer one million table records in 10,000 increments with select fields from mysql to postgresql without pandas

from sqlalchemy import create_engine postgres_str = 'postgresql://localhost/postgres' mysql_str = 'mysql+pymysql://localhost/mysql' mysql_engine = create_engine(mysql_str) postgres_engine = create_engine(postgres_str) sql = 'select * from tablename' def insert_sql_table_to_postgres(column_names, sql, mysql_engine, postgres_engine): """ column_names: list of the column names from the mysql table sql: string of sql to select from the mysql table mysql_engine: sqlalchemy engine for mysql postgres_engine: sqlalchemy engine for postgres """ df = pd.read_sql(sql, mysql_engine) df = df.head(1000000) conn = postgres_engine.raw_connection() cursor = conn.cursor() chunks = list(pd.read_sql(sql, mysql

Questions about programming?Chat with your personal AI assistant