Changes for page 01 Lua Functions

Last modified by Devin Chen on 2026/08/18 14:08

From version 1.1
edited by Wecon
on 2025/09/03 21:04
Change comment: Imported from XAR
To version 6.1
edited by Devin Chen
on 2026/08/18 14:08
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
... ... @@ -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
... ... @@ -1355,10 +1355,27 @@
1355 1355  end
1356 1356  {{/code}}
1357 1357  
1358 -(((
1359 -
1360 -)))
1360 +{{info}}
1361 +If your Lua script throws the error **module 'http' not found** or **module 'https' not found**, please first check the firmware version of the V‑BOX.
1361 1361  
1363 +For H series & E series (E01 firmware), use the scripts below to load the corresponding modules:
1364 +
1365 +~-~- Load required modules for HTTP requests
1366 +local http = require("socket.http")
1367 +
1368 +~-~- Load required modules for HTTPS requests
1369 +local https = require("https")
1370 +
1371 +For E series (E02 firmware), use the scripts below to load the corresponding modules:
1372 +
1373 +​​~-~- Load required modules for HTTP requests
1374 +local http = require("socket.http")
1375 +
1376 +~-~- Load required modules for HTTPS requests
1377 +local https = require("socket.https")
1378 +{{/info}}
1379 +
1380 +
1362 1362  (((
1363 1363  = **8 General Functions** =
1364 1364  )))
... ... @@ -1742,7 +1742,7 @@
1742 1742  end
1743 1743  {{/code}}
1744 1744  
1745 -== **sha(string message** ==
1764 +== **sha(string message)** ==
1746 1746  
1747 1747  **Function:** SHA calculate
1748 1748  
... ... @@ -1779,3 +1779,127 @@
1779 1779  
1780 1780  end
1781 1781  {{/code}}
1801 +
1802 += **11 MySQL database operation** =
1803 +
1804 +{{info}}
1805 +Note: E series V-box with E02 firmware doesn't support
1806 +{{/info}}
1807 +
1808 +== **luaMySql.init(string sourcename, string username, string password, string host, number port, string character)** ==
1809 +
1810 +**Function:** Configure database connection parameters
1811 +
1812 +**Parameters:**
1813 +
1814 +sourcename: Database name
1815 +
1816 +username: Connection username
1817 +
1818 +password: Connection password
1819 +
1820 +host: Connection host name
1821 +
1822 +port: Connection host port
1823 +
1824 +character: Character set used for the connection
1825 +
1826 +**Returns:**
1827 +
1828 +Success: true
1829 +
1830 +Failure: multi
1831 +
1832 +== **luaMySql.exec(string statement)** ==
1833 +
1834 +**Function:** Execute the given SQL statement (no result set required, e.g., insert, delete, update)
1835 +
1836 +**Parameters:**
1837 +
1838 +statement: The given SQL statement
1839 +
1840 +**Returns:**
1841 +
1842 +Success: status (Returns the number of rows affected by the SQL statement execution)
1843 +
1844 +Failure: nil, errorString
1845 +
1846 +== **luaMySql.execWithResult(string statement)** ==
1847 +
1848 +**Function:** Execute the given SQL statement (result set required, e.g., query)
1849 +
1850 +**Parameters:**
1851 +
1852 +statement: The given SQL statement
1853 +
1854 +**Returns:**
1855 +
1856 +Success: table (Returns the result set)
1857 +
1858 +Failure: nil, errorString
1859 +
1860 +
1861 +**Example:**
1862 +
1863 +**{{code language="lua"}}mysql = require "mysqlclient"
1864 +
1865 +function DataInitRight()
1866 + local dbName = "db_lua1"
1867 + local user = "root"
1868 + local pwd = "123456"
1869 + local host = "192.168.56.186"
1870 + local port = 3306
1871 + local character = "UTF8"
1872 + mysql.init(dbName, user, pwd, host, port, character)
1873 +end
1874 +
1875 +function ExecFunc()
1876 + status, errorString = mysql.exec("delete from tb_lua1 where mykey = 10;")
1877 + if nil == status then
1878 + print("ExecFunc() error:", errorString)
1879 + return -1
1880 + else
1881 + print("the number of rows affected by the command:", status)
1882 + end
1883 + return 0
1884 +end
1885 +
1886 +function ExecWithResultFunc()
1887 + status, errorString = mysql.execWithResult("select * from tb_lua1;")
1888 + if nil == status then
1889 + print("ExecWithResultFunc() error:", errorString)
1890 + return -1
1891 + else
1892 + print("ExecWithResultFunc() success: status type =", type(status))
1893 + print("ExecWithResultFunc() success: status len =", #status)
1894 +
1895 + local num = #status
1896 + local i = 1
1897 + if num > 0 then
1898 + for i = 1, num, 1 do
1899 + local var = string.format("select result[%d] :mykey = %d, value = %s",
1900 + i, status[i].mykey, status[i].value)
1901 + print(var)
1902 + end
1903 + end
1904 + print("---------------")
1905 + end
1906 + return 0
1907 +end
1908 +
1909 +function luaMysql_apiTest.main()
1910 + print("script running ...")
1911 + DataInitRight()
1912 +
1913 + -- use exec demo
1914 + if ExecFunc() < 0 then
1915 + return
1916 + end
1917 +
1918 + -- use execWithResult demo
1919 + if ExecWithResultFunc() < 0 then
1920 + return
1921 + end
1922 +
1923 + print("script running success")
1924 +end{{/code}}**