Error Embedded Python Code - sqlalchemy

questions about GAMS' tools
Post Reply
NikosM
User
User
Posts: 3
Joined: 7 months ago

Error Embedded Python Code - sqlalchemy

Post by NikosM » 3 weeks ago

Hello,
I am trying to retrieve data from a database table using sqlalchemy

Code: Select all

Set
sec sectors     
;

$onEmbeddedCode Python:
import sqlalchemy
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mssql://LAPTOP-HdfDNQL8E4/POLIFOLIO_DB?driver=SQL+Server+Native+Client+11.0?trusted_connection=yes')
sec = pd.read_sql_query('select distinct cast(SECTORID AS INT) from pf_actions', engine)
df = pd.DataFrame(sec)
gams.set('sec', [tuple(x) for x in df.values.tolist()])
$offEmbeddedCode sec
sectorid is a column with decimals.
Unfortunately it returns an error
Cannot load/unload symbol with unknown dimension:

How can i load my data to "sec" set?

Lutz
User
User
Posts: 29
Joined: 3 years ago

Re: Error Embedded Python Code - sqlalchemy

Post by Lutz » 2 weeks ago

Hi,

As the error message indicates, you get the problem, since GAMS does not know the dimension of the symbol "sec" in your code. You can set it, for example, in the declaration like this to define "sec" as one dimensional set:

Code: Select all

Set
sec(*) sectors     
;
Hope that helps,
Lutz

Post Reply