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"

  1. /**Custom Code*/
  2. Object current = param(1);
  3. treenode activity = param(2);
  4. Token token = param(3);
  5. treenode processFlow = ownerobject(activity);
  6.  
  7.  
  8. Database.Connection dbConnection_1 = Database.Connection("DBConnector1");
  9.  
  10. // Make sure it's connected
  11. if(dbConnection_1.isConnected == 0)
  12. {
  13. dbConnection_1.connect();
  14. }
  15.  
  16. // Confirm that it was successful
  17. if(dbConnection_1.isConnected == 0)
  18. {
  19. print("Not connected");
  20. }
  21. else
  22. {
  23. print("Connected");
  24. }
  25.  
  26. // Get the data from the SQL database
  27. Database.ResultSet queryResult = dbConnection_1.query("SELECT * FROM TestDataForFlexSimConnection");
  28.  
  29. // Echo the data to confirm
  30. while(queryResult.fetchNext())
  31. {
  32. print(queryResult["TestCol1"], " ", queryResult["TestCol2"], " ", queryResult["TestCol3"]);
  33. }
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.

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.