-1
I’m going from SQL to python and I’m still a little confused. It is the following here in my code I want to know if the values of the select of the result variable exist in the select of the sale variable, and if they exist do nothing, and if they do not exist load the data. In sql I used If exists, I don’t know what is the equivalent in python. Thanks in advance.
Example I am bringing record of an sql table and loading in another Mysql table, ai in case you already have these records in mysql table I want to print on screen: no new record, and in case you have record that are not yet in Mysql table want to upload these records.
Note: I play the data in the dataframe to then load in mysql
import pymysql.Cursors import pyodbc import pandas as pd from sqlalchemy import create_engine
connection = pyodbc.connect("DSN=SQLServer") #autocommit=True
try:
with connection.cursor() as cursor:
result = "SELECT * FROM dw.dbo.vW_vendas"
df = pd.read_sql_query("SELECT * FROM dw.dbo.vW_Vendas",connection,index_col=None,coerce_float=True, parse_dates= 'DataBaseContrato')
cursor.execute(result)
table = cursor.fetchall()
print(table)
finally:
connection.close()
#Conexão Mysql
cnx = create_engine('mysql+pymysql://teste:teste@teste/dw')
cnxmysql = pymysql.connect(host='teste',
user='teste',
password='teste',
db='dw')
try:
with cnxmysql.cursor() as cursor2:
venda = "SELECT * FROM ft_venda_teste"
cursor2.execute(venda)
venda = cursor2.fetchall()
print(venda)
finally:
cnxmysql.close()
df.to_sql(con=cnx, name= 'ft_venda_teste',if_exists= 'replace', index= False)
print('dados Carregados')
Got a little confused, you want to check if records exist in the table at the time of select?
– Sidon
That’s right, Example I’m bringing record of an sql table and loading into another Mysql table, ai in case you already have these record in the mysql table I want you to print on the screen: no new record, and in case you have record that are not yet in the Mysql table I want you to load these records.
– Nidorus