Search code examples
mysqllualuasql

Lua Script can't connect to MySQL-Database


I'm following the Lua part of this tutorial: http://wiki.dragino.com/index.php?title=Save_Data_to_MySQL.

Especially this code:

require "luasql.mysql"
env = assert (luasql.mysql())
con = assert (env:connect"nkt_development",'db_user','db_passwordL','172.31.10.60',3306)

Unfortunately I got an error that I can't fix

lua: mysql_test.lua:7: attempt to index global 'luasql' (a nil value)
stack traceback:
        mysql_test.lua:7: in main chunk
        [C]: ?

I am working on an dragino gatway / Arduino Yun.


Solution

  • I found the answer myself by typing the following into the first line. Instead of just requiring the libary I had to declare it onto a variable to use later in the code.

    luasql = require "luasql.mysql"

    This is what my final code looks like:

    luasql = require "luasql.mysql"
    value=arg[1]
    current_time=os.date("%Y-%m-%d %H:%M:%S")
    env = luasql.mysql()
    con = assert (env:connect('development', 'DBUSER', 'PASSWORD', 'HOSTIP','3306'))
    res = assert (con:execute('INSERT INTO record(time,value) VALUES("'..current_time..'",'..value..')'))