Changes for page 2 Script

Last modified by Devin Chen on 2025/06/06 14:03

From version 66.1
edited by Theodore Xu
on 2023/08/25 09:17
Change comment: There is no comment for this version
To version 70.1
edited by Theodore Xu
on 2023/08/25 09:48
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1484,14 +1484,12 @@
1484 1484  
1485 1485  == **2.7 Mysql** ==
1486 1486  
1487 -
1488 -
1489 1489  === 1.Install Mysql software ===
1490 1490  
1491 1491  Download the latest version of Mysql.
1492 1492  
1493 1493  (% style="text-align:center" %)
1494 -[[image:Mysql的软件版本.png||height="673" width="894"]]
1492 +[[image:Mysql的软件版本.png]]
1495 1495  
1496 1496  === 2.Navicat connect Mysql ===
1497 1497  
... ... @@ -1498,21 +1498,21 @@
1498 1498  Connecting to Mysql with navicat.
1499 1499  
1500 1500  (% style="text-align:center" %)
1501 -[[image:How to connect MYSQL with V-Box@连接到mysql.png]]
1499 +[[image:连接到mysql.png]]
1502 1502  
1503 -=== 3.Create a database ===
1501 +=== 3.Create database ===
1504 1504  
1505 1505  Character set should be choose utf8mb3/utf8mb4 and Collation choose utf8mb3_danish_ci/utf8mb4_danish_ci.
1506 1506  
1507 1507  (% style="text-align:center" %)
1508 -[[image:How to connect MYSQL with V-Box@创建数据库.png||height="585" width="818"]]
1506 +[[image:创建数据库.png]]
1509 1509  
1510 -=== 4.Create a data table ===
1508 +=== 4.Create data table ===
1511 1511  
1512 1512  Create a table, enter the fields you need.
1513 1513  
1514 1514  (% style="text-align:center" %)
1515 -[[image:创建表1.png||height="484" width="737"]]
1513 +[[image:创建表1.png]]
1516 1516  
1517 1517  Save, enter table name.
1518 1518  
... ... @@ -1524,12 +1524,135 @@
1524 1524  (% style="text-align:center" %)
1525 1525  [[image:输入或者导入数据.png]]
1526 1526  
1527 -=== 6.Modifying scripts and debug:**[[Mysql script>>https://docs.we-con.com.cn/bin/view/V-BOX/V-Net/Manual/04%20Lua%20Script/01%20Lua%20Functions/\#H11MySQLdatabaseoperation]]** ===
1525 +=== 6.Script ===
1528 1528  
1527 +**luaMySql.init(string sourcename, string username, string password, string host, number port, string character)**
1528 +
1529 +**Function:** Configure database connection parameters
1530 +
1531 +**Parameter:**
1532 +
1533 +sourcename: the name of database
1534 +
1535 +username: the username of the connection
1536 +
1537 +password: the password of the connection
1538 +
1539 +host: the host name of the connection
1540 +
1541 +port: the host port of the connection
1542 +
1543 +character: the character set of the connection
1544 +
1545 +**Return:**
1546 +
1547 +Succeed: string
1548 +
1549 +Failed: multi
1550 +
1551 +**luaMySql.exec(string statement)**
1552 +
1553 +**Function:** Execute the given SQL statement without returning the result set (add, delete, change)
1554 +
1555 +**Parameter:**
1556 +
1557 +statement: the given SQL statement
1558 +
1559 +**Return:**
1560 +
1561 +Succeed: status: returns the number of rows affected by SQL statement execution.
1562 +
1563 +Failed: nil, errorString
1564 +
1565 +**luaMySql.execWithResult(string statement)**
1566 +
1567 +**Function:** Execute the given SQL statement returning the result set (check)
1568 +
1569 +**Parameter:**
1570 +
1571 +statement: the given SQL statement
1572 +
1573 +**Return:**
1574 +
1575 +Succeed: table: returns the result set
1576 +
1577 +Failed: nil, errorString
1578 +
1579 +**For example:**
1580 +
1581 +
1582 +{{code language="LUA"}}
1583 +-- Assuming the "mysqlclient" library is properly installed and available
1584 +mysql = require("mysqlclient")
1585 +
1586 +function DataInitRight()
1587 + local dbName = "excel"
1588 + local user = "root"
1589 + local pwd = "XXXXX"
1590 + local host = "192.168.39.146"
1591 + local port = 3306
1592 + local character = "utf8mb3"
1593 +
1594 + mysql.init(dbName, user, pwd, host, port, character)
1595 +end
1596 +
1597 +function ExecFunc()
1598 + status, errorString = mysql.exec("delete from student where Name = 'XXX';") --Delete statement, column name = table element
1599 + if nil == status then
1600 + print("ExecFunc() error:", errorString)
1601 + return -1
1602 + else
1603 + print("the number of rows affected by the command:", status)
1604 + end
1605 + return 0
1606 +end
1607 +
1608 +
1609 +function ExecWithResultFunc()
1610 + status, errorString = mysql.execWithResult("select * from student;")
1611 + if nil == status then
1612 + print("ExecWithResultFunc() error:", errorString)
1613 + return -1
1614 + else
1615 + print("ExecWithResultFunc() success : status type = ", type(status))
1616 + print("ExecWithResultFunc() success : status len = ", #status)
1617 + local num = #status
1618 + local i = 1
1619 + if num > 0 then
1620 + for i = 1, num, 1 do
1621 + 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
1622 + print(var)
1623 + end
1624 + end
1625 + print("---------------")
1626 + end
1627 + return 0
1628 +end
1629 +
1630 +function MySQL.main()
1631 + print("script running ...")
1632 + DataInitRight()
1633 +
1634 + -- use exec demo
1635 + if ExecFunc() < 0 then
1636 + return
1637 + end
1638 +
1639 + -- use execWithResult demo
1640 + if ExecWithResultFunc() < 0 then
1641 + return
1642 + end
1643 +
1644 + print("script running success")
1645 +end
1646 +{{/code}}
1647 +
1648 +=== 7.Debug ===
1649 +
1529 1529  (% style="text-align:center" %)
1530 1530  [[image:调试结果.png]]
1531 1531  
1532 -=== 7.Problem ===
1653 +=== 8.Problem ===
1533 1533  
1534 1534  During our debugging process it may there are some problems about Mysql, such as
1535 1535  
Mysql的软件版本.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +78.4 KB
Content
创建数据库.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +53.2 KB
Content
创建表1.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +49.0 KB
Content
创建表2.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +18.4 KB
Content
调试结果.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +68.4 KB
Content
输入或者导入数据.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +26.6 KB
Content
连接到mysql.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +59.0 KB
Content
连接数据库1.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +56.5 KB
Content
连接数据库2.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.AiXia
Size
... ... @@ -1,0 +1,1 @@
1 +25.5 KB
Content