Changes for page 01 Lua Functions

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

From version 20.1
edited by Theodore Xu
on 2023/08/25 09:49
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
... ... @@ -1872,8 +1872,130 @@
1872 1872  
1873 1873  Failed: nil
1874 1874  
1875 -= **11 Message summary algorithm** =
1875 += **11 MySQL database operation** =
1876 1876  
1877 +== **luaMySql.init(string sourcename, string username, string password, string host, number port, string character)** ==
1878 +
1879 +**Function:** Configure database connection parameters
1880 +
1881 +**Parameter:**
1882 +
1883 +sourcename: the name of database
1884 +
1885 +username: the username of the connection
1886 +
1887 +password: the password of the connection
1888 +
1889 +host: the host name of the connection
1890 +
1891 +port: the host port of the connection
1892 +
1893 +character: the character set of the connection
1894 +
1895 +**Return:**
1896 +
1897 +Succeed: string
1898 +
1899 +Failed: multi
1900 +
1901 +== **luaMySql.exec(string statement)** ==
1902 +
1903 +**Function:** Execute the given SQL statement without returning the result set (add, delete, change)
1904 +
1905 +**Parameter:**
1906 +
1907 +statement: the given SQL statement
1908 +
1909 +**Return:**
1910 +
1911 +Succeed: status: returns the number of rows affected by SQL statement execution.
1912 +
1913 +Failed: nil, errorString
1914 +
1915 +== **luaMySql.execWithResult(string statement)** ==
1916 +
1917 +**Function:** Execute the given SQL statement returning the result set (check)
1918 +
1919 +**Parameter:**
1920 +
1921 +statement: the given SQL statement
1922 +
1923 +**Return:**
1924 +
1925 +Succeed: table: returns the result set
1926 +
1927 +Failed: nil, errorString
1928 +
1929 +**For example:**
1930 +
1931 +{{code language="LUA"}}
1932 +-- Assuming the "mysqlclient" library is properly installed and available
1933 +mysql = require("mysqlclient")
1934 +
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)
1944 +end
1945 +
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
1955 +end
1956 +
1957 +
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
1977 +end
1978 +
1979 +function MySQL.main()
1980 + print("script running ...")
1981 + DataInitRight()
1982 +
1983 + -- use exec demo
1984 + if ExecFunc() < 0 then
1985 + return
1986 + end
1987 +
1988 + -- use execWithResult demo
1989 + if ExecWithResultFunc() < 0 then
1990 + return
1991 + end
1992 +
1993 + print("script running success")
1994 +end
1995 +{{/code}}
1996 +
1997 += **12 Message summary algorithm** =
1998 +
1877 1877  == **hmac(string hash_func, string key, string message)** ==
1878 1878  
1879 1879  **Function:** HMAC calculate