Changes for page 01 Lua Functions

Last modified by Theodore Xu on 2023/10/26 10:51

From version 17.1
edited by Theodore Xu
on 2023/08/24 16:45
Change comment: There is no comment for this version
To version 10.1
edited by Hunter
on 2023/01/15 16:45
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.AiXia
1 +XWiki.Hunter
Content
... ... @@ -1508,12 +1508,9 @@
1508 1508  * Obtaining ICCID (read only)
1509 1509  ** addr_getstring("@W_HSW225",15)
1510 1510  * (((
1511 -TSAP settings of Siemens LOGO PLC*
1512 -
1513 -(((
1511 +TSAP settings of Siemens LOGO PLC* (((
1514 1514  addr_setword("@W_0#HSW1200",8192) means set the Local TSAP as 20.00
1515 1515  )))
1516 -
1517 1517  * (((
1518 1518  addr_setword("@W_0#HSW1201",4096) means set the Remote TSAP as 10.00
1519 1519  )))
... ... @@ -1929,69 +1929,125 @@
1929 1929  **For example:**
1930 1930  
1931 1931  {{code language="LUA"}}
1932 --- Assuming the "mysqlclient" library is properly installed and available
1933 -mysql = require("mysqlclient")
1929 +mysql = require"mysqlclient"
1934 1934  
1935 1935  function DataInitRight()
1936 - local dbName = "excel"
1937 - local user = "root"
1938 - local pwd = "XXXXX"
1939 - local host = "192.168.39.146"
1940 - local port = 3306
1941 - local character = "utf8mb3"
1942 -
1943 - mysql.init(dbName, user, pwd, host, port, character)
1932 +
1933 +local dbName = "db_lua1"
1934 +
1935 +local user = "root"
1936 +
1937 +local pwd = "123456"
1938 +
1939 +local host = "192.168.56.186"
1940 +
1941 +local port = 3306
1942 +local character = "UTF8"
1943 +
1944 +mysql.init(dbName, user, pwd, host, port, character)
1945 +
1944 1944  end
1945 1945  
1946 1946  function ExecFunc()
1947 - status, errorString = mysql.exec("delete from student where Name = 'XXX';") --Delete statement, column name = table element
1948 - if nil == status then
1949 - print("ExecFunc() error:", errorString)
1950 - return -1
1951 - else
1952 - print("the number of rows affected by the command:", status)
1953 - end
1954 - return 0
1949 +
1950 +status, errorString = mysql.exec("delete from tb_lua1 where mykey =
1951 +
1952 +10;")
1953 +
1954 +if nil == status then
1955 +
1956 +print("ExecFunc() error:", errorString)
1957 +
1958 +return -1
1959 +
1960 +else
1961 +
1962 +print("the number of rows affected by the command:", status)
1963 +
1955 1955  end
1956 1956  
1966 +return 0
1957 1957  
1968 +end
1969 +
1958 1958  function ExecWithResultFunc()
1959 - status, errorString = mysql.execWithResult("select * from student;")
1960 - if nil == status then
1961 - print("ExecWithResultFunc() error:", errorString)
1962 - return -1
1963 - else
1964 - print("ExecWithResultFunc() success : status type = ", type(status))
1965 - print("ExecWithResultFunc() success : status len = ", #status)
1966 - local num = #status
1967 - local i = 1
1968 - if num > 0 then
1969 - for i = 1, num, 1 do
1970 - local var = string.format("select result[%d] :Num = %d,Name = %s,Age = %d", i, status[i].Num, status[i].Name,status[i].Age) --Iterate through the data in the table, noting whether the elements are strings or numbers
1971 - print(var)
1972 - end
1973 - end
1974 - print("---------------")
1975 - end
1976 - return 0
1971 +
1972 +status, errorString = mysql.execWithResult("select * from tb_lua1;")
1973 +
1974 +if nil == status then
1975 +
1976 +print("ExecWithResultFunc() error:", errorString)
1977 +
1978 +return -1
1979 +
1980 +else
1981 +
1982 +print("ExecWithResultFunc()
1983 +
1984 +success
1985 +
1986 +: status
1987 +
1988 +type
1989 +
1990 +=
1991 +
1992 +",
1993 +
1994 +type(status))
1995 +
1996 +print("ExecWithResultFunc() success : status len = ", #status)
1997 +
1998 +local num = #status
1999 +
2000 +local i = 1
2001 +
2002 +if num > 0 then
2003 +
2004 +for i = 1, num, 1 do
2005 +
2006 +local var = string.format("select result[%d] :mykey = %d,
2007 +
2008 +value = %s", i, status[i].mykey, status[i].value)
2009 +
2010 +print(var)
2011 +
1977 1977  end
1978 1978  
1979 -function MySQL.main()
1980 - print("script running ...")
1981 - DataInitRight()
2014 +end
1982 1982  
1983 - -- use exec demo
1984 - if ExecFunc() < 0 then
1985 - return
1986 - end
2016 +print("---------------")
1987 1987  
1988 - -- use execWithResult demo
1989 - if ExecWithResultFunc() < 0 then
1990 - return
1991 - end
2018 +end
1992 1992  
1993 - print("script running success")
2020 +return 0
1994 1994  end
2022 +
2023 +function luaMysql_apiTest.main()
2024 +
2025 +print("script running ...")
2026 +
2027 +DataInitRight()
2028 +
2029 +--use exec demo
2030 +
2031 +if ExecFunc() < 0 then
2032 +
2033 +return
2034 +
2035 +end
2036 +
2037 +--use execWithResult demo
2038 +
2039 +if ExecWithResultFunc() < 0 then
2040 +
2041 +return
2042 +
2043 +end
2044 +
2045 +print("script running success")
2046 +
2047 +end
1995 1995  {{/code}}
1996 1996  
1997 1997  = **12 Message summary algorithm** =