Changes for page 01 Lua Functions

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

From version 16.1
edited by Theodore Xu
on 2023/08/24 16:41
Change comment: There is no comment for this version
To version 17.1
edited by Theodore Xu
on 2023/08/24 16:45
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1929,125 +1929,69 @@
1929 1929  **For example:**
1930 1930  
1931 1931  {{code language="LUA"}}
1932 -mysql = require"mysqlclient"
1932 +-- Assuming the "mysqlclient" library is properly installed and available
1933 +mysql = require("mysqlclient")
1933 1933  
1934 1934  function DataInitRight()
1935 -
1936 -local dbName = "db_lua1"
1937 -
1938 -local user = "root"
1939 -
1940 -local pwd = "123456"
1941 -
1942 -local host = "192.168.56.186"
1943 -
1944 -local port = 3306
1945 -local character = "UTF8"
1946 -
1947 -mysql.init(dbName, user, pwd, host, port, character)
1948 -
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)
1949 1949  end
1950 1950  
1951 1951  function ExecFunc()
1952 -
1953 -status, errorString = mysql.exec("delete from tb_lua1 where mykey =
1954 -
1955 -10;")
1956 -
1957 -if nil == status then
1958 -
1959 -print("ExecFunc() error:", errorString)
1960 -
1961 -return -1
1962 -
1963 -else
1964 -
1965 -print("the number of rows affected by the command:", status)
1966 -
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
1967 1967  end
1968 1968  
1969 -return 0
1970 1970  
1971 -end
1972 -
1973 1973  function ExecWithResultFunc()
1974 -
1975 -status, errorString = mysql.execWithResult("select * from tb_lua1;")
1976 -
1977 -if nil == status then
1978 -
1979 -print("ExecWithResultFunc() error:", errorString)
1980 -
1981 -return -1
1982 -
1983 -else
1984 -
1985 -print("ExecWithResultFunc()
1986 -
1987 -success
1988 -
1989 -: status
1990 -
1991 -type
1992 -
1993 -=
1994 -
1995 -",
1996 -
1997 -type(status))
1998 -
1999 -print("ExecWithResultFunc() success : status len = ", #status)
2000 -
2001 -local num = #status
2002 -
2003 -local i = 1
2004 -
2005 -if num > 0 then
2006 -
2007 -for i = 1, num, 1 do
2008 -
2009 -local var = string.format("select result[%d] :mykey = %d,
2010 -
2011 -value = %s", i, status[i].mykey, status[i].value)
2012 -
2013 -print(var)
2014 -
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
2015 2015  end
2016 2016  
2017 -end
1979 +function MySQL.main()
1980 + print("script running ...")
1981 + DataInitRight()
2018 2018  
2019 -print("---------------")
1983 + -- use exec demo
1984 + if ExecFunc() < 0 then
1985 + return
1986 + end
2020 2020  
2021 -end
1988 + -- use execWithResult demo
1989 + if ExecWithResultFunc() < 0 then
1990 + return
1991 + end
2022 2022  
2023 -return 0
1993 + print("script running success")
2024 2024  end
2025 -
2026 -function luaMysql_apiTest.main()
2027 -
2028 -print("script running ...")
2029 -
2030 -DataInitRight()
2031 -
2032 ---use exec demo
2033 -
2034 -if ExecFunc() < 0 then
2035 -
2036 -return
2037 -
2038 -end
2039 -
2040 ---use execWithResult demo
2041 -
2042 -if ExecWithResultFunc() < 0 then
2043 -
2044 -return
2045 -
2046 -end
2047 -
2048 -print("script running success")
2049 -
2050 -end
2051 2051  {{/code}}
2052 2052  
2053 2053  = **12 Message summary algorithm** =