Page 1 of 1

Error Embedded Python Code - sqlalchemy

Posted: Thu Sep 19, 2019 10:11 pm
by NikosM
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?

Re: Error Embedded Python Code - sqlalchemy

Posted: Tue Sep 24, 2019 10:43 am
by Lutz
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