Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2398 Visualizzazioni

I want to sync data table from a SQL server db table to a Odoo database table.

the synchronization should be automatic or there can be an on-click button to sync the data manually.

What are the ways to do this task?

I heard about xmlrpc port, psycopg, and creating a script.  but don't know how  to use them

Need some suggestions

Avatar
Abbandona

I think you should first start learning how to do it using whenever you wanna start with and ask specific questions. Otherwise you wouldn't get any help because your question it's too ambiguous and seems like you wanna others to give you a solution for something unclear

Autore

Axel Mendoza, Thanks for your reply.

This is what I have done so far. My objective is to transfer the data from a SQL server to odoo Postgres database automatically. below is my Python script, which will run on scheduled basis.

Now I'm trying to know, what is the industry standard, how are you solving this problem, is there any better way to do this.

import pandas as pd
import pymssql
import psycopg2
import psycopg2.extras

# Function to fetch data from SQL Server data table
def fetch_data_from_sql_server(server, database, username, password, table_name):
connection = pymssql.connect(server=server, database=database, user=username, password=password)
query = f'SELECT TOP 15 CustomerCode,CustomerName,Mobile,Business FROM {table_name};' # Use "TOP 10" to get the first 10 rows
data = pd.read_sql(query, connection)
connection.close()
return data

# Function to insert data into PostgreSQL database
def insert_data_to_postgres(server, database, username, password, table_name, data):
connection_string = f'dbname={database} user={username} password={password} host={server}'
with psycopg2.connect(connection_string) as conn:
data_columns = data.columns.tolist()

# Convert data to a list of tuples to use with psycopg2.extras.execute_values
data_values = [tuple(row) for row in data.to_numpy()]

with conn.cursor() as cur:
for row in data_values:
customer_code = row[0] # Assuming CustomerCode is the first column in the DataFrame
# Check if the CustomerCode exists in the PostgreSQL table
cur.execute(f"SELECT CustomerCode FROM {table_name} WHERE CustomerCode = %s", (customer_code,))
if not cur.fetchone(): # If the customer does not exist, insert the row
insert_query = f"INSERT INTO {table_name} ({', '.join(data_columns)}) VALUES %s"
psycopg2.extras.execute_values(cur, insert_query, [row])

# Commit the changes to the database
conn.commit()

# Configuration for SQL Server
sql_server_config = {
"server": "server_url",
"database": "dbname",
"username": "sa",
"password": "password",
"table_name": "Customer",
}

# Configuration for PostgreSQL
postgres_config = {
"server": "server_url",
"database": "dbname",
"username": "username",
"password": "password",
"table_name": "table_name",
}

# Fetch data from SQL Server
data = fetch_data_from_sql_server(**sql_server_config)

# Insert data into PostgreSQL
insert_data_to_postgres(**postgres_config, data=data)

print("Data transfer completed successfully.")

Post correlati Risposte Visualizzazioni Attività
2
ott 25
5417
1
ott 25
4144
1
mar 24
2592
1
nov 22
6783
0
dic 21
35