Hello Guest it is March 28, 2024, 10:31:11 AM

Author Topic: Sending Info to a Database from a Script  (Read 5671 times)

0 Members and 1 Guest are viewing this topic.

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Sending Info to a Database from a Script
« Reply #10 on: February 07, 2019, 08:43:36 PM »
wxLua is LUA with bindings to the wxWidgets library.  You don't necessarily HAVE to use the wxWidgets bindings.  Just don't write code that uses anything with the "wx." prefix. 

The database should be doable.  We provide luasql for mySQL and ODBC.  However, neither will won't work by itself!  Database client software also needs to be installed.  For instance, if you installed an IBM DB2 client, it would install the ODBC driver.  For mySQL, the client files need to be installed on the machine, namely "libmysql.dll".  And these DLLs need to be in the PATH as well so that the luasql driver can find them.  Or copy them into the Mach4 installation folder.  But it is best to put their orignal location in the PATH environment variable though.

Attached is an example file from https://keplerproject.github.io/luasql/examples.html that I munged up to include our module paths.  It assumes that there is a database called "luasql-test" available that requires no password to access and alter.  I set one up and the script ran flawlessly. 

A mySQL database can also be accessed via ODBC with the following: https://dev.mysql.com/downloads/connector/odbc/  Install that and configure.  Then change the mysql specific stuff in that example file from myslq to odbc (lines 5 and 7). 

Steve
Re: Sending Info to a Database from a Script
« Reply #11 on: February 08, 2019, 02:47:01 PM »
Thank you Smurph.

understood wx is Graphical - like wxWidgets python etc

Yes I have Installed MySQL 8.0 , We installed all drivers except the c++ library ; since I will also access this database with .NET. and was not sure how Lua would connect - being new to mach4 I saw the python folders and said better to have more installed than less - while I try to make this work.

Because LuaSQL via Luarocks was not installing - maybe I did not need to do that ...
We went to the odbc driver for a MySQL connection and set up the connection in the windows ODBC connection.


I will double check to see if MySQL installer placed them in the %path% variable but I think that is correct - I try to point to the original rather than a copy located elsewhere - as upgrades may / will require that specific version. Just easier.


" I set one up and the script ran flawlessly. "
Did you run the script from with in Mach4 ? as a Macro or regular .LUA script  ?

I intend to call this with a MCode from a GCode script that our machinist wrote .

I meant to ask about this line of code
package.path = package.path .. ";.\\Modules\\?.lua;"

It seems to be a generic folder  where we point to .Lua files could you give a quick what it is , and that syntax represents ??



 
« Last Edit: February 08, 2019, 02:49:18 PM by KenSchnell »

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Sending Info to a Database from a Script
« Reply #12 on: February 08, 2019, 03:48:53 PM »
Because LuaSQL via Luarocks was not installing - maybe I did not need to do that ...
We went to the odbc driver for a MySQL connection and set up the connection in the windows ODBC connection.

We provide the luasql modules in the Mach installation forlder's "Modules" directory.  Luarocks isn't needed or desired unless you have a separate LUA installation that you also want to have lausql connectivity. 

I will double check to see if MySQL installer placed them in the %path% variable but I think that is correct - I try to point to the original rather than a copy located elsewhere - as upgrades may / will require that specific version. Just easier.

The MySQL installer didn't put ANYTHING in my PATH.  I had to add it myself.  My installation is in C:\MySql, so what I added to the PATH environment variable was "C:\MySQL\lib", as that is where the client DLL files were.  I also added "C:\MySQL\bin" for grins. 

" I set one up and the script ran flawlessly. "
Did you run the script from with in Mach4 ? as a Macro or regular .LUA script  ?

I ran the script from the Zerobrane editor.  Mach running, do the menu path Operator -> Open Script Editor.  Then open the file from my last post.  You can then step through the program line by line.  Hit F5 to start the program.  It will pause on the first line, then hit F10 to advance to the next line.  This is called single stepping.  Anyway, when it gets to the line where it "requires" the module step past that.  If you get an error there then that means luasql can't find the libmysql.dll dependency.  The error message is misleading.  It says that lua can't find luasql\mysql.dll in the specified directory.  However, if you look in that directory, it is indeed there.  So yes, that is misleading.  But that is more of a Windows thing than a LUA thing. 

I intend to call this with a MCode from a GCode script that our machinist wrote .

That will work.  I ran it from within the editor to test it.  But that code could be put into a macro script and it would be fine. 

I meant to ask about this line of code
package.path = package.path .. ";.\\Modules\\?.lua;"

It seems to be a generic folder  where we point to .Lua files could you give a quick what it is , and that syntax represents ??

Well.  those are the lines I added to the example on the luasql site.  That is the LUA equivalent of the PATH environment variable (package.path and package.cpath).  They are search directories.  So what those lines do is add our Modules directory to the directories that are searched when we try and load a module.  So lets examine the string that we appended:

";.\\Modules\\?.lua;"

";" is a path separator, just like what is used in the PATH environment variable. 
"." means "current directory".  This is used when we provide a relative path.  An absolute path could be used as well, such as "C:\\Mach4Hobby\\Modules\\?.lua".  But the relative path is used because we always have the Mach installation directory as the current working directory when launching scripts. 
"\\Modules\\" is the part of the path that points to the Modules directory.
"?.lua;" specifies the valid file extention. 

A lot of times you might do something like this:

package.path = package.path .. ";.\\Modules\\?.lua;.\\Modules\\?.mcs"

This will look for files with *.lua and *.mcs extentions. 

package.cpath will always need to look for *.dll files on Windows (*.so files on Linux). 

Anyway, look in your Mach installation's Modules directory to see what all we provide for extending LUA.  There are modules like luars232 for serial comms and luasockets for network comms, etc...

Steve
Re: Sending Info to a Database from a Script
« Reply #13 on: February 08, 2019, 06:00:40 PM »
 :)AWESOME :) Thank you Smurph - this should now be plenty to get us going .. my first experience with Mach4, and Lua ...

Thanks for the explanation of the path variable..
Re: Sending Info to a Database from a Script
« Reply #14 on: February 11, 2019, 05:12:46 PM »
When running the code below from the Zerobrane Studio:

package.path = package.path .. ";.\\Modules\\?.lua;.\\Modules\\?.mcs"

-- mysql.dll and odbc.dll are in the addons path so I included a direct reference search path of AddOns

package.cpath = package.cpath .. ";.\\Modules\\?.dll;.\\Modules\\AddOns\\?.dll"

-- load driver
local driver = require "luasql.odbc"
-- if I run this in the mcLUAEditor - debug the script exits with exit code -1 (I assume that means it could not find the driver
-- my macros are located here  ..       C:\Mach4\Profiles\Mill\Macros


-- create environment object
env = assert (driver.odbc())
-- connect to data source
con = assert (env:connect("mycnc_v1"))
-- reset our table


  res = assert (con:execute(string.format([[
    INSERT INTO operator
    VALUES ('Me', 'Myself')]], FirstName, LastName)
  ))

 
con:close()
env:close()



I get the Error output :

Program starting as '"C:\Mach4\ZeroBraneStudio\bin\lua52.exe" -e "io.stdout:setvbuf('no')" "C:\Users\MACHMO~1\AppData\Local\Temp\.3F36.tmp"'.
Program 'lua52.exe' started in 'C:\Mach4\Profiles\Mill\Macros' (pid: 3588).
C:\Mach4\ZeroBraneStudio\bin\lua52.exe: C:\Users\MACHMO~1\AppData\Local\Temp\.3F36.tmp:1: module 'machipc' not found:
   no field package.preload['machipc']
   no file 'C:\Mach4\Modules/machipc.lua'
   no file 'C:\Mach4\ZeroBraneStudio\bin\lua\machipc.lua'
   no file 'C:\Mach4\ZeroBraneStudio\bin\lua\machipc\init.lua'
   no file 'C:\Mach4\ZeroBraneStudio\bin\machipc.lua'
   no file 'C:\Mach4\ZeroBraneStudio\bin\machipc\init.lua'
   no file '.\machipc.lua'
   no file './machipc.lua'
   no file './machipc/init.lua'
   no file './lua/machipc.lua'
   no file './lua/machipc/init.lua'
   no file 'C:\Mach4\ZeroBraneStudio\lualibs/machipc/machipc.lua'
   no file 'C:\Mach4\ZeroBraneStudio\lualibs/machipc.lua'
   no file 'C:\Mach4\ZeroBraneStudio\lualibs/machipc/machipc/init.lua'
   no file 'C:\Mach4\ZeroBraneStudio\lualibs/machipc/init.lua'
   no file 'C:\Mach4\Modules/machipc.dll'
   no file 'C:\Mach4\ZeroBraneStudio\bin/clibs52/machipc.dll'
stack traceback:
   [C]: in function 'require'
   C:\Users\MACHMO~1\AppData\Local\Temp\.3F36.tmp:1: in main chunk
   [C]: in ?
Program completed in 0.19 seconds (pid: 3588).

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Sending Info to a Database from a Script
« Reply #15 on: February 12, 2019, 12:38:19 AM »
What version (build) of Mach are you running?  The luasql module have never been in the addons directory to my knowledge. 

Should be:
C:\<machDir>\Modules\luasql\myslq.dll
and
C:\<machDir>\Modules\luasql\odbc.dll

Steve
Re: Sending Info to a Database from a Script
« Reply #16 on: February 12, 2019, 09:52:14 AM »
We have Mach 4.2.0.3804 Build 3804

I do not have a luasql directory ..

How can I install that ?

Can you zip that up and I can then install it ? - I assume luasql does not need to be registered in windows in order for it to work.
« Last Edit: February 12, 2019, 10:02:05 AM by KenSchnell »

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Sending Info to a Database from a Script
« Reply #17 on: February 14, 2019, 12:33:15 AM »
Well...  I guess I put all of those modules in after 3804!  :(  You could try one of the development versions from the FTP site.  4088 is the most recent.  And by all accounts it is looking pretty good. 

Steve
Re: Sending Info to a Database from a Script
« Reply #18 on: February 14, 2019, 08:01:42 AM »
Thank you.. I have downloaded the latest - I was checking the change log and 3139 - 3113 LuaSQL was added - however mine is 3804 and does not have it - so maybe it missed the files/folders inclusion in the build process. Anyway I have downloaded the Latest and Greatest .. hoping I can simply move the folders directory and do what I need to do as the current system is running -  - and I do not want to 'fix' it to broken. We will see how it performs thanks again! :)
Re: Sending Info to a Database from a Script
« Reply #19 on: February 14, 2019, 04:35:11 PM »
Hello - Smurph ..
Having an issue still - I tried my code and received an error - the module could not be found.
So I put your code in with no changes and I received the same result.
 I skipped the dynamics and set the path physically C:\Mach4Industrial\Modules\?.dll , also the same for the package.path  except ?.lua at the end
I have had the same results same error - the module is there in the path - the 4.20.0.4088 installer placed two files in the luasql folder mysql.dll and odbc.dll
When I try the odbc driver I get procedure not found. I tried running these form both mcLuaEditor and zerobrane with the exact same errors - respectively.

I checked folder permissions and so I set them for everyone - I am running zerobrane and mcluaeditor (Run As Administrator)  and I am still not passed this point. On my development machine I am running in demo mode, should not matter - but then again maybe..

Debugging session started in 'C:\Mach4Industrial\'.
error loading module 'luasql.mysql' from file 'C:\Mach4Industrial\Modules/luasql\mysql.dll':
   The specified module could not be found.