question

Damodaran Moturu G avatar image
0 Likes"
Damodaran Moturu G asked Michael Machado commented

Import data to global table from database

Hi,

I have global tables in my model and as of today I'm importing data using excel import option. Going forward, I will have to get the data from SQL Server database instead of excel. I could not find any documentation or Q&A on how to import data to global tables from database. Please advise whether it is possible to import data to global table from database. If yes, would be grateful if you could guide me with sample code.

Thanks

Damu

FlexSim 18.0.0
database import
5 |100000

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

Mischa Spelt avatar image
4 Likes"
Mischa Spelt answered ShabDz edited

Here is a bit of code I wrote a while ago to read the result of a query into a Global Table in the model.

It's fairly well-commented (even for me) so hopefully you will be able to figure it out.

  1. if(dbgetmode() == DBMODE_CLOSED)
  2. {
  3. showprogressbar("Connecting to ODBC-connection 'ODBC Name'...");
  4.  
  5.  
  6. dbopen("ODBC Name", "<SQL Query here>", 0);
  7. }
  8.  
  9.  
  10. showprogressbar("Importing data from the database...");
  11.  
  12.  
  13. // Store the size of the query result
  14. int rows = dbgetnumrows();
  15. int cols = dbgetnumcols();
  16.  
  17.  
  18. // Clear the bundle
  19. Table target = Table("Result");
  20. target.setSize(0, 0);
  21.  
  22.  
  23. // We can save ourselves work if there is no data
  24. // Plus I want to be sure that we have at least one row of data from now on.
  25. if(rows > 0)
  26. {
  27. // Figure out the column types based on the first record, and add columns to our bundle
  28. for(int i = 1; i <= cols; i++)
  29. {
  30. int type = stringtonum(dbgettablestr(1, i)) > 0 ? BUNDLE_FIELD_TYPE_DOUBLE : BUNDLE_FIELD_TYPE_STR;
  31. addbundlefield(target, dbgetfieldname(i), type );
  32. }
  33.  
  34.  
  35. // Now create enough rows for all the data
  36. target.setSize(rows, cols + 1);
  37. // Fill the data column by column so we only have to check each column type once
  38. // We expect about 30k rows, so every 1k rows should be informative and still smooth.
  39. for(int j = 1; j <= cols; j++)
  40. {
  41. int type = getbundlefieldtype(target, j - 1);
  42. for(int i = 1; i <= rows; i++)
  43. {
  44. // Updating the progress bar on EVERY record makes it a bit slow.
  45. if(i % 1000 == 0)
  46. {
  47. setprogressbar( 100 * ((j - 1) * rows + i) / (rows * cols) );
  48. }
  49. // This is a bit ugly but there is no dbgettableval(i, j)
  50. // Does FlexScript have branch prediction? ;-)
  51. if(type == BUNDLE_FIELD_TYPE_STR)
  52. {
  53. target[i][j] = dbgettablestr(i, j);
  54. }
  55. else
  56. {
  57. target[i][j] = dbgettablenum(i, j);
  58. }
  59. } // for(i)
  60. } // for(j)
  61. } // rows > 0
  62.  
  63.  
  64. hideprogressbar();
  65.  
  66.  
  67. // Calls AssignPallets();
  68. resetmodel();
  69.  
  70.  
  71. if(msg("Close database?", "Do you want to close the database? If you want to read from the database again later on, it may be faster to keep it open. Select Yes to close the database, No to keep it open.", 4))
  72. {
  73. dbclose();
  74. }
  75.  
  76.  
· 4
5 |100000

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

Ben Wilson avatar image
1 Like"
Ben Wilson answered Damodaran Moturu G commented

Hi @Damodaran Moturu G,

This MySQL+ODBC tutorial can walk you through the basics of connecting an external database software to FlexSim. It is not a perfect match - it looks like you'll be using a different database software, but the basic principles will be the same.

The main difference is that you will need to find the proper ODBC driver for your database application.

Comment back if you get stuck or have any questions connecting your database.

If you have other questions unrelated to the top post, please ask them in a new question.

Thanks!

· 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.