Changes for page 01 Lua Functions
Last modified by Devin Chen on 2025/09/24 15:40
To version 3.1
edited by Devin Chen
on 2025/09/24 15:40
on 2025/09/24 15:40
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. Wecon1 +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,123 @@ 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 +**Function:** Configure database connection parameters 1788 + 1789 +**Parameters:** 1790 + 1791 +sourcename: Database name 1792 + 1793 +username: Connection username 1794 + 1795 +password: Connection password 1796 + 1797 +host: Connection host name 1798 + 1799 +port: Connection host port 1800 + 1801 +character: Character set used for the connection 1802 + 1803 +**Returns:** 1804 + 1805 +Success: true 1806 + 1807 +Failure: multi 1808 + 1809 +== **luaMySql.exec(string statement)** == 1810 + 1811 +**Function:** Execute the given SQL statement (no result set required, e.g., insert, delete, update) 1812 + 1813 +**Parameters:** 1814 + 1815 +statement: The given SQL statement 1816 + 1817 +**Returns:** 1818 + 1819 +Success: status (Returns the number of rows affected by the SQL statement execution) 1820 + 1821 +Failure: nil, errorString 1822 + 1823 +== **luaMySql.execWithResult(string statement)** == 1824 + 1825 +**Function:** Execute the given SQL statement (result set required, e.g., query) 1826 + 1827 +**Parameters:** 1828 + 1829 +statement: The given SQL statement 1830 + 1831 +**Returns:** 1832 + 1833 +Success: table (Returns the result set) 1834 + 1835 +Failure: nil, errorString 1836 + 1837 + 1838 +**Example:** 1839 + 1840 +**{{code language="lua"}}mysql = require "mysqlclient" 1841 + 1842 +function DataInitRight() 1843 + local dbName = "db_lua1" 1844 + local user = "root" 1845 + local pwd = "123456" 1846 + local host = "192.168.56.186" 1847 + local port = 3306 1848 + local character = "UTF8" 1849 + mysql.init(dbName, user, pwd, host, port, character) 1850 +end 1851 + 1852 +function ExecFunc() 1853 + status, errorString = mysql.exec("delete from tb_lua1 where mykey = 10;") 1854 + if nil == status then 1855 + print("ExecFunc() error:", errorString) 1856 + return -1 1857 + else 1858 + print("the number of rows affected by the command:", status) 1859 + end 1860 + return 0 1861 +end 1862 + 1863 +function ExecWithResultFunc() 1864 + status, errorString = mysql.execWithResult("select * from tb_lua1;") 1865 + if nil == status then 1866 + print("ExecWithResultFunc() error:", errorString) 1867 + return -1 1868 + else 1869 + print("ExecWithResultFunc() success: status type =", type(status)) 1870 + print("ExecWithResultFunc() success: status len =", #status) 1871 + 1872 + local num = #status 1873 + local i = 1 1874 + if num > 0 then 1875 + for i = 1, num, 1 do 1876 + local var = string.format("select result[%d] :mykey = %d, value = %s", 1877 + i, status[i].mykey, status[i].value) 1878 + print(var) 1879 + end 1880 + end 1881 + print("---------------") 1882 + end 1883 + return 0 1884 +end 1885 + 1886 +function luaMysql_apiTest.main() 1887 + print("script running ...") 1888 + DataInitRight() 1889 + 1890 + -- use exec demo 1891 + if ExecFunc() < 0 then 1892 + return 1893 + end 1894 + 1895 + -- use execWithResult demo 1896 + if ExecWithResultFunc() < 0 then 1897 + return 1898 + end 1899 + 1900 + print("script running success") 1901 +end{{/code}}**