Pasar DataFrame a SQL con Python [Código] (Pandas)

					

import pandas as pd
data = pd.read_csv (r'C:\Users\SOSSA\Desktop\mascotas-2021-06-02.CSV')   
df = pd.DataFrame(data, columns= ['Ranking','Dato1','Dato2',"Fecha"])
print(df)
# Example Python program to serialize a pandas DataFrame
# into a PostgreSQL table
from sqlalchemy import create_engine
import psycopg2
import pandas as pds      
alchemyEngine = create_engine('postgresql+psycopg2://admin:1234@127.0.0.1/productos', pool_recycle=3600);
postgreSQLConnection = alchemyEngine.connect();
postgreSQLTable = "data1";
try:
    frame = df.to_sql(postgreSQLTable, postgreSQLConnection,  if_exists = 'append');
except ValueError as vx:
    print(vx)
except Exception as ex:  
    print(ex)
else:
    print("Datos %s metidos en la tabla."%postgreSQLTable);
finally:
    postgreSQLConnection.close();

boton entrar

Deja un comentario