Changes for page 01 Lua Functions

Last modified by Devin Chen on 2025/09/24 15:40

From version 1.1
edited by Wecon
on 2025/09/03 21:04
Change comment: Imported from XAR
To version 2.1
edited by Devin Chen
on 2025/09/24 15:34
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.Wecon
1 +XWiki.DevinChen
Content
... ... @@ -1742,7 +1742,7 @@
1742 1742  end
1743 1743  {{/code}}
1744 1744  
1745 -== **sha(string message** ==
1745 +== **sha(string message)** ==
1746 1746  
1747 1747  **Function:** SHA calculate
1748 1748  
... ... @@ -1779,3 +1779,127 @@
1779 1779  
1780 1780  end
1781 1781  {{/code}}
1782 +
1783 += **11 MySQL database operation** =
1784 +
1785 +== **luaMySql.init(string sourcename, string username, string password, string host, number port, string character)** ==
1786 +
1787 +
1788 +**Function:** Configure database connection parameters
1789 +
1790 +**Parameters:**
1791 +
1792 +
1793 +sourcename: Database name
1794 +
1795 +username: Connection username
1796 +
1797 +password: Connection password
1798 +
1799 +host: Connection host name
1800 +
1801 +port: Connection host port
1802 +
1803 +character: Character set used for the connection
1804 +
1805 +**Returns:**
1806 +
1807 +Success: true
1808 +
1809 +Failure: multi
1810 +
1811 +== **luaMySql.exec(string statement)** ==
1812 +
1813 +**Function:** Execute the given SQL statement (no result set required, e.g., insert, delete, update)
1814 +
1815 +**Parameters:**
1816 +
1817 +
1818 +statement: The given SQL statement
1819 +
1820 +**Returns:**
1821 +
1822 +Success: status (Returns the number of rows affected by the SQL statement execution)
1823 +
1824 +Failure: nil, errorString
1825 +
1826 +== **luaMySql.execWithResult(string statement)** ==
1827 +
1828 +**Function:** Execute the given SQL statement (result set required, e.g., query)
1829 +
1830 +**Parameters:**
1831 +
1832 +
1833 +statement: The given SQL statement
1834 +
1835 +**Returns:**
1836 +
1837 +Success: table (Returns the result set)
1838 +
1839 +Failure: nil, errorString
1840 +
1841 +
1842 +**Example:**
1843 +
1844 +**{{code language="lua"}}mysql = require "mysqlclient"
1845 +
1846 +function DataInitRight()
1847 + local dbName = "db_lua1"
1848 + local user = "root"
1849 + local pwd = "123456"
1850 + local host = "192.168.56.186"
1851 + local port = 3306
1852 + local character = "UTF8"
1853 + mysql.init(dbName, user, pwd, host, port, character)
1854 +end
1855 +
1856 +function ExecFunc()
1857 + status, errorString = mysql.exec("delete from tb_lua1 where mykey = 10;")
1858 + if nil == status then
1859 + print("ExecFunc() error:", errorString)
1860 + return -1
1861 + else
1862 + print("the number of rows affected by the command:", status)
1863 + end
1864 + return 0
1865 +end
1866 +
1867 +function ExecWithResultFunc()
1868 + status, errorString = mysql.execWithResult("select * from tb_lua1;")
1869 + if nil == status then
1870 + print("ExecWithResultFunc() error:", errorString)
1871 + return -1
1872 + else
1873 + print("ExecWithResultFunc() success: status type =", type(status))
1874 + print("ExecWithResultFunc() success: status len =", #status)
1875 +
1876 + local num = #status
1877 + local i = 1
1878 + if num > 0 then
1879 + for i = 1, num, 1 do
1880 + local var = string.format("select result[%d] :mykey = %d, value = %s",
1881 + i, status[i].mykey, status[i].value)
1882 + print(var)
1883 + end
1884 + end
1885 + print("---------------")
1886 + end
1887 + return 0
1888 +end
1889 +
1890 +function luaMysql_apiTest.main()
1891 + print("script running ...")
1892 + DataInitRight()
1893 +
1894 + -- use exec demo
1895 + if ExecFunc() < 0 then
1896 + return
1897 + end
1898 +
1899 + -- use execWithResult demo
1900 + if ExecWithResultFunc() < 0 then
1901 + return
1902 + end
1903 +
1904 + print("script running success")
1905 +end{{/code}}**