question

Craig DIckson avatar image
0 Likes"
Craig DIckson asked Craig DIckson answered

SQL from Database Connector works but not from code

I am trying to learn how to use the SQL server connection. I will be doing many different queries so I am trying to learn how to query via code. I have SQL Server, and was successfully able to set it up in the Database Connector: the Test Connection button returned "Connection succeeded" and I was able to use the import tab to get the data into a global table.

But as I said I wanted to do it by code. So I put the code below in a ProcessFlow Custom Code activity, but got this error:

"time: 0.000000 exception: FlexScript exception: 42000 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Statement(s) could not be prepared. at MODEL:/Tools/ProcessFlow/ProcessFlow/Custom Code>variables/codeNode"

/**Custom Code*/
Object current = param(1);
treenode activity = param(2);
Token token = param(3);
treenode processFlow = ownerobject(activity);


Database.Connection dbConnection_1 = Database.Connection("DBConnector1");

// Make sure it's connected
if(dbConnection_1.isConnected == 0)
{
	dbConnection_1.connect();
}

// Confirm that it was successful
if(dbConnection_1.isConnected == 0)
{
	print("Not connected");
}
else
{
	print("Connected");
}

// Get the data from the SQL database
Database.ResultSet queryResult = dbConnection_1.query("SELECT * FROM TestDataForFlexSimConnection");

// Echo the data to confirm
while(queryResult.fetchNext())
{
	print(queryResult["TestCol1"], " ", queryResult["TestCol2"], " ", queryResult["TestCol3"]);
}
It confirmed that it did successfully connect (using the print statements) so it's not that. Since it works from the DatabaseConnector, I'm hoping it's something small I missed?
FlexSim 18.2.2
sql serverodbc
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jordan Johnson avatar image Jordan Johnson ♦♦ commented ·

Can you post a model? It would help to see how your connection is configured.

0 Likes 0 ·

1 Answer

Craig DIckson avatar image
0 Likes"
Craig DIckson answered

OK, I found my mistake. I used the database name in my query when I should have used the table name. Duh.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.