Changes for page 01 Lua Functions
Last modified by Devin Chen on 2025/11/26 18:12
To version 5.1
edited by Devin Chen
on 2025/11/26 18:12
on 2025/11/26 18:12
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
-
... ... @@ -765,7 +765,7 @@ 765 765 1: reserved 766 766 767 767 ((( 768 -== **mqtt.create(string serverurl, string clientid)** == 768 +== **mqtt.create(string serverurl, string clientid[, int useDomain])** == 769 769 ))) 770 770 771 771 **Function:** Create an MQTT object ... ... @@ -784,6 +784,8 @@ 784 784 785 785 //clientid//: Client ID 786 786 787 +//int useDomain: //0: convert to IP for connection, 1: use domain name for connection. 788 + 787 787 **Return:** 788 788 789 789 Succeed: MQTT object ... ... @@ -1742,7 +1742,7 @@ 1742 1742 end 1743 1743 {{/code}} 1744 1744 1745 -== **sha(string message** == 1747 +== **sha(string message)** == 1746 1746 1747 1747 **Function:** SHA calculate 1748 1748 ... ... @@ -1779,3 +1779,127 @@ 1779 1779 1780 1780 end 1781 1781 {{/code}} 1784 + 1785 += **11 MySQL database operation** = 1786 + 1787 +{{info}} 1788 +Note: E series V-box with E02 firmware doesn't support 1789 +{{/info}} 1790 + 1791 +== **luaMySql.init(string sourcename, string username, string password, string host, number port, string character)** == 1792 + 1793 +**Function:** Configure database connection parameters 1794 + 1795 +**Parameters:** 1796 + 1797 +sourcename: Database name 1798 + 1799 +username: Connection username 1800 + 1801 +password: Connection password 1802 + 1803 +host: Connection host name 1804 + 1805 +port: Connection host port 1806 + 1807 +character: Character set used for the connection 1808 + 1809 +**Returns:** 1810 + 1811 +Success: true 1812 + 1813 +Failure: multi 1814 + 1815 +== **luaMySql.exec(string statement)** == 1816 + 1817 +**Function:** Execute the given SQL statement (no result set required, e.g., insert, delete, update) 1818 + 1819 +**Parameters:** 1820 + 1821 +statement: The given SQL statement 1822 + 1823 +**Returns:** 1824 + 1825 +Success: status (Returns the number of rows affected by the SQL statement execution) 1826 + 1827 +Failure: nil, errorString 1828 + 1829 +== **luaMySql.execWithResult(string statement)** == 1830 + 1831 +**Function:** Execute the given SQL statement (result set required, e.g., query) 1832 + 1833 +**Parameters:** 1834 + 1835 +statement: The given SQL statement 1836 + 1837 +**Returns:** 1838 + 1839 +Success: table (Returns the result set) 1840 + 1841 +Failure: nil, errorString 1842 + 1843 + 1844 +**Example:** 1845 + 1846 +**{{code language="lua"}}mysql = require "mysqlclient" 1847 + 1848 +function DataInitRight() 1849 + local dbName = "db_lua1" 1850 + local user = "root" 1851 + local pwd = "123456" 1852 + local host = "192.168.56.186" 1853 + local port = 3306 1854 + local character = "UTF8" 1855 + mysql.init(dbName, user, pwd, host, port, character) 1856 +end 1857 + 1858 +function ExecFunc() 1859 + status, errorString = mysql.exec("delete from tb_lua1 where mykey = 10;") 1860 + if nil == status then 1861 + print("ExecFunc() error:", errorString) 1862 + return -1 1863 + else 1864 + print("the number of rows affected by the command:", status) 1865 + end 1866 + return 0 1867 +end 1868 + 1869 +function ExecWithResultFunc() 1870 + status, errorString = mysql.execWithResult("select * from tb_lua1;") 1871 + if nil == status then 1872 + print("ExecWithResultFunc() error:", errorString) 1873 + return -1 1874 + else 1875 + print("ExecWithResultFunc() success: status type =", type(status)) 1876 + print("ExecWithResultFunc() success: status len =", #status) 1877 + 1878 + local num = #status 1879 + local i = 1 1880 + if num > 0 then 1881 + for i = 1, num, 1 do 1882 + local var = string.format("select result[%d] :mykey = %d, value = %s", 1883 + i, status[i].mykey, status[i].value) 1884 + print(var) 1885 + end 1886 + end 1887 + print("---------------") 1888 + end 1889 + return 0 1890 +end 1891 + 1892 +function luaMysql_apiTest.main() 1893 + print("script running ...") 1894 + DataInitRight() 1895 + 1896 + -- use exec demo 1897 + if ExecFunc() < 0 then 1898 + return 1899 + end 1900 + 1901 + -- use execWithResult demo 1902 + if ExecWithResultFunc() < 0 then 1903 + return 1904 + end 1905 + 1906 + print("script running success") 1907 +end{{/code}}**