ms-vite-plugin 1.4.49 → 1.4.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/build.js +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/mcp/doc-tools.js +1 -1
  4. package/dist/mcp/docs-service.js +1 -1
  5. package/dist/mcp/types.d.ts +1 -1
  6. package/dist/mcp/types.js +1 -1
  7. package/dist/project.js +1 -1
  8. package/docs/AGENTS.md +3 -2
  9. package/docs/SKILL.md +2 -1
  10. package/docs/apilua/action.md +1013 -0
  11. package/docs/apilua/appleocr.md +281 -0
  12. package/docs/apilua/cloud.md +183 -0
  13. package/docs/apilua/config.md +181 -0
  14. package/docs/apilua/cryptoUtils.md +253 -0
  15. package/docs/apilua/device.md +485 -0
  16. package/docs/apilua/dotocr.md +230 -0
  17. package/docs/apilua/file.md +554 -0
  18. package/docs/apilua/global.md +654 -0
  19. package/docs/apilua/hid.md +1126 -0
  20. package/docs/apilua/hotUpdate.md +167 -0
  21. package/docs/apilua/http.md +427 -0
  22. package/docs/apilua/image.md +1177 -0
  23. package/docs/apilua/ime.md +283 -0
  24. package/docs/apilua/logger.md +294 -0
  25. package/docs/apilua/media.md +283 -0
  26. package/docs/apilua/mysql.md +445 -0
  27. package/docs/apilua/netCard.md +224 -0
  28. package/docs/apilua/node.md +264 -0
  29. package/docs/apilua/opencv.md +870 -0
  30. package/docs/apilua/paddleocr.md +324 -0
  31. package/docs/apilua/pip.md +359 -0
  32. package/docs/apilua/system.md +686 -0
  33. package/docs/apilua/tomatoocr.md +461 -0
  34. package/docs/apilua/tts.md +330 -0
  35. package/docs/apilua/ui.md +1033 -0
  36. package/docs/apilua/utils.md +222 -0
  37. package/docs/apilua/yolo.md +324 -0
  38. package/docs/apilua/yolocls.md +276 -0
  39. package/docs/mcp-agent-description.md +5 -4
  40. package/docs/quick/vscode/createProject.md +84 -0
  41. package/docs/quick/vscode/packProject.md +17 -0
  42. package/package.json +2 -1
@@ -0,0 +1,283 @@
1
+ # 媒体模块 (Media)
2
+
3
+ 媒体模块提供了丰富的媒体操作功能,包括图片和视频的相册管理,以及音频播放控制等功能。
4
+
5
+ ## 功能概览
6
+
7
+ - **相册管理**: 保存图片和视频到相册,清理相册内容
8
+ - **音频播放**: MP3 音频的播放、停止和循环控制
9
+ - **同步/异步**: 支持异步播放和同步等待播放结束
10
+
11
+ ## API 参考
12
+
13
+ ### 相册操作
14
+
15
+ #### saveImageToAlbum - 保存图像到系统相册。
16
+
17
+ ```lua
18
+ ---@param imageId string
19
+ ---@return boolean
20
+ function saveImageToAlbum(imageId) end
21
+ ```
22
+
23
+ **参数:**
24
+
25
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
26
+ | --------- | ------ | -------- | ------ | ------------------------------ |
27
+ | `imageId` | string | 是 | | 图像 ID(通过 image 模块获取) |
28
+
29
+ **返回值:**
30
+
31
+ | 类型 | 描述 |
32
+ | --------- | ------------ |
33
+ | `boolean` | 是否保存成功 |
34
+
35
+ **示例:**
36
+
37
+ ```lua
38
+ local media = require("media")
39
+ -- 保存截图到相册
40
+ media.saveImageToAlbum("screen")
41
+
42
+ local documentsDir = file.getInternalDir("documents")
43
+ -- 读取图像并保存到相册
44
+ local imageId = image.readImage(tostring(documentsDir) .. "/screenshot.png")
45
+ if imageId then
46
+ local saved = media.saveImageToAlbum(imageId)
47
+ if saved then
48
+ print("图片已保存到相册")
49
+ else
50
+ print("保存失败")
51
+ end
52
+ end
53
+ ```
54
+
55
+ #### saveVideoToAlbumPath - 保存视频文件到系统相册。
56
+
57
+ ```lua
58
+ ---@param path string
59
+ ---@return boolean
60
+ function saveVideoToAlbumPath(path) end
61
+ ```
62
+
63
+ **参数:**
64
+
65
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
66
+ | ------ | ------ | -------- | ------ | ------------ |
67
+ | `path` | string | 是 | | 视频文件路径 |
68
+
69
+ **返回值:**
70
+
71
+ | 类型 | 描述 |
72
+ | --------- | ------------ |
73
+ | `boolean` | 是否保存成功 |
74
+
75
+ **示例:**
76
+
77
+ ```lua
78
+ local media = require("media")
79
+ -- 保存视频到相册 (从 documents 目录读取)
80
+ local documentsDir = file.getInternalDir("documents")
81
+ local videoName = "video.mp4"
82
+ local videoPath = tostring(documentsDir) .. "/" .. tostring(videoName)
83
+ local saved = media.saveVideoToAlbumPath(videoPath)
84
+ if saved then
85
+ print("视频已保存到相册")
86
+ else
87
+ print("保存失败")
88
+ end
89
+ ```
90
+
91
+ #### deleteAllPhotos - 清空相册中的所有图片。
92
+
93
+ ```lua
94
+ ---@return boolean
95
+ function deleteAllPhotos() end
96
+ ```
97
+
98
+ **返回值:**
99
+
100
+ | 类型 | 描述 |
101
+ | --------- | ------------ |
102
+ | `boolean` | 是否删除成功 |
103
+
104
+ **示例:**
105
+
106
+ ```lua
107
+ local media = require("media")
108
+ -- 清空相册图片
109
+ local deleted = media.deleteAllPhotos()
110
+ if deleted then
111
+ print("相册图片已清空")
112
+ else
113
+ print("清空失败")
114
+ end
115
+ ```
116
+
117
+ #### deleteAllVideos - 清空相册中的所有视频。
118
+
119
+ ```lua
120
+ ---@return boolean
121
+ function deleteAllVideos() end
122
+ ```
123
+
124
+ **返回值:**
125
+
126
+ | 类型 | 描述 |
127
+ | --------- | ------------ |
128
+ | `boolean` | 是否删除成功 |
129
+
130
+ **示例:**
131
+
132
+ ```lua
133
+ local media = require("media")
134
+ -- 清空相册视频
135
+ local deleted = media.deleteAllVideos()
136
+ if deleted then
137
+ print("相册视频已清空")
138
+ else
139
+ print("清空失败")
140
+ end
141
+ ```
142
+
143
+ #### deleteAllScreenshots - 清空相册中的所有截图。
144
+
145
+ ```lua
146
+ ---@return boolean
147
+ function deleteAllScreenshots() end
148
+ ```
149
+
150
+ **示例:**
151
+
152
+ ```lua
153
+ local media = require("media")
154
+ local deleted = media.deleteAllScreenshots()
155
+ if deleted then
156
+ print("相册截图已清空")
157
+ end
158
+ ```
159
+
160
+ ### 音频播放
161
+
162
+ #### playMp3 - 异步播放 MP3 音频文件。
163
+
164
+ ```lua
165
+ ---@param path string
166
+ ---@param loop boolean
167
+ ---@return boolean
168
+ function playMp3(path, loop) end
169
+ ```
170
+
171
+ **参数:**
172
+
173
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
174
+ | ------ | ------- | -------- | ------ | ------------ |
175
+ | `path` | string | 是 | | MP3 文件路径 |
176
+ | `loop` | boolean | 是 | | 是否循环播放 |
177
+
178
+ **返回值:**
179
+
180
+ | 类型 | 描述 |
181
+ | --------- | ---------------- |
182
+ | `boolean` | 是否开始播放成功 |
183
+
184
+ **示例:**
185
+
186
+ ```lua
187
+ local media = require("media")
188
+ -- 播放背景音乐(循环)
189
+ local played = media.playMp3("/path/to/background.mp3", true)
190
+ if played then
191
+ print("背景音乐开始播放")
192
+ end
193
+ -- 播放音效(不循环)
194
+ local soundPlayed = media.playMp3("/path/to/sound.mp3", false)
195
+ if soundPlayed then
196
+ print("音效播放中")
197
+ end
198
+ ```
199
+
200
+ #### stopMp3 - 停止当前播放的 MP3 音频。
201
+
202
+ ```lua
203
+ ---@return boolean
204
+ function stopMp3() end
205
+ ```
206
+
207
+ **返回值:**
208
+
209
+ | 类型 | 描述 |
210
+ | --------- | ------------ |
211
+ | `boolean` | 是否停止成功 |
212
+
213
+ **示例:**
214
+
215
+ ```lua
216
+ local media = require("media")
217
+ -- 停止音乐播放
218
+ local stopped = media.stopMp3()
219
+ if stopped then
220
+ print("音乐已停止")
221
+ else
222
+ print("停止失败或没有正在播放的音乐")
223
+ end
224
+ ```
225
+
226
+ #### playMp3WaitEnd - 同步播放 MP3 音频文件(播放一次并等待结束)。
227
+
228
+ ```lua
229
+ ---@param path string
230
+ ---@return boolean
231
+ function playMp3WaitEnd(path) end
232
+ ```
233
+
234
+ **参数:**
235
+
236
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
237
+ | ------ | ------ | -------- | ------ | ------------ |
238
+ | `path` | string | 是 | | MP3 文件路径 |
239
+
240
+ **返回值:**
241
+
242
+ | 类型 | 描述 |
243
+ | --------- | -------------------------------------------------------------------- |
244
+ | `boolean` | 是否正常播放到结尾;系统音频中断、`stopMp3` 或新播放替换时返回 `false` |
245
+
246
+ **示例:**
247
+
248
+ ```lua
249
+ local media = require("media")
250
+ -- 同步播放音频,等待播放完成
251
+ print("开始播放音频...")
252
+ local played = media.playMp3WaitEnd("/path/to/notification.mp3")
253
+ if played then
254
+ print("音频播放完成")
255
+ else
256
+ print("播放失败或被系统音频中断")
257
+ end
258
+ ```
259
+
260
+
261
+ #### isMp3Playing - 当前是否正在播放 MP3。
262
+
263
+ ```lua
264
+ ---@return boolean
265
+ function isMp3Playing() end
266
+ ```
267
+
268
+ **返回值:**
269
+
270
+ | 类型 | 描述 |
271
+ | --------- | ---------------------------- |
272
+ | `boolean` | 正在播放返回 `true`,否则 `false` |
273
+
274
+ **示例:**
275
+
276
+ ```lua
277
+ local media = require("media")
278
+ if media.isMp3Playing() then
279
+ print("正在播放")
280
+ else
281
+ print("未播放")
282
+ end
283
+ ```
@@ -0,0 +1,445 @@
1
+ # MySQL 数据库模块 (MySQL)
2
+
3
+ MySQL 模块提供了完整的 MySQL 数据库连接和操作功能,支持连接管理、查询执行、事务处理等核心数据库操作。
4
+
5
+ ## 功能概览
6
+
7
+ - **连接管理**: 创建、连接、断开数据库连接
8
+ - **数据查询**: 执行 SELECT 查询操作
9
+ - **数据更新**: 执行 CREATE INSERT、UPDATE、DELETE 操作
10
+ - **事务支持**: 完整的事务管理功能
11
+ - **参数绑定**: 支持参数化查询,防止 SQL 注入
12
+
13
+ ## 类型定义
14
+
15
+ ### MySQLConfig
16
+
17
+ 数据库连接配置接口。
18
+
19
+ ```lua
20
+ ---@class MySQLConfig
21
+ ---@field host string
22
+ ---@field port number
23
+ ---@field username string
24
+ ---@field password string
25
+ ---@field database string
26
+ ---@field charset string
27
+ ---@field connectTimeout number
28
+ ---@field queryTimeout number
29
+ ```
30
+
31
+ **数据类型说明:**
32
+
33
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
34
+ | ---------------- | ------ | -------- | ------- | ----------------------------- |
35
+ | `host` | string | 是 | | 数据库主机地址,必填 |
36
+ | `port` | number | 否 | 3306 | 数据库端口,默认 3306 |
37
+ | `username` | string | 是 | | 用户名,必填 |
38
+ | `password` | string | 是 | | 密码,必填 |
39
+ | `database` | string | 是 | | 数据库名,必填 |
40
+ | `charset` | string | 否 | utf8mb4 | 字符集,默认 utf8mb4 |
41
+ | `connectTimeout` | number | 否 | 5000 | 连接超时时间,默认 5000 毫秒 |
42
+ | `queryTimeout` | number | 否 | 30000 | 查询超时时间,默认 30000 毫秒 |
43
+
44
+ ### MySQLResult
45
+
46
+ 查询结果接口。
47
+
48
+ ```lua
49
+ ---@class MySQLResult
50
+ ---@field rows table[]
51
+ ---@field affectedRows number
52
+ ---@field insertId number
53
+ ---@field success boolean
54
+ ---@field error string
55
+ ```
56
+
57
+ **数据类型说明:**
58
+
59
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
60
+ | -------------- | --------------------- | -------- | ------ | ------------------------------- |
61
+ | `rows` | table[] | 否 | | 查询结果数据行 |
62
+ | `affectedRows` | number | 否 | | 受影响的行数 |
63
+ | `insertId` | number | 否 | | 插入 ID(仅适用于 INSERT 操作) |
64
+ | `success` | boolean | 否 | | 查询是否成功 |
65
+ | `error` | string | 否 | | 错误信息 |
66
+
67
+ ## API 参考
68
+
69
+ ### 连接管理
70
+
71
+ #### connect - 连接到数据库
72
+
73
+ ```lua
74
+ ---@param config MySQLConfig
75
+ ---@return boolean
76
+ function connect(config) end
77
+ ```
78
+
79
+ **参数:**
80
+
81
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
82
+ | -------- | ----------- | -------- | ------ | ------------------ |
83
+ | `config` | MySQLConfig | 是 | | 数据库连接配置对象 |
84
+
85
+ **返回值:**
86
+
87
+ | 类型 | 描述 |
88
+ | --------- | ------------ |
89
+ | `boolean` | 是否连接成功 |
90
+
91
+ **示例:**
92
+
93
+ ```lua
94
+ local mysql = require("mysql")
95
+ local success = mysql.connect({
96
+ host = "localhost",
97
+ port = 3306,
98
+ username = "root",
99
+ password = "password",
100
+ database = "test_db",
101
+ charset = "utf8mb4",
102
+ connectTimeout = 5000,
103
+ queryTimeout = 30000,
104
+ })
105
+
106
+ if success then
107
+ print("数据库连接成功")
108
+ else
109
+ print("数据库连接失败")
110
+ end
111
+ ```
112
+
113
+ #### disconnect - 断开数据库连接
114
+
115
+ ```lua
116
+ function disconnect() end
117
+ ```
118
+
119
+ **示例:**
120
+
121
+ ```lua
122
+ local mysql = require("mysql")
123
+ mysql.disconnect()
124
+ print("数据库连接已断开")
125
+ ```
126
+
127
+ #### isConnected - 检查连接状态
128
+
129
+ ```lua
130
+ ---@return boolean
131
+ function isConnected() end
132
+ ```
133
+
134
+ **返回值:**
135
+
136
+ | 类型 | 描述 |
137
+ | --------- | ---------- |
138
+ | `boolean` | 是否已连接 |
139
+
140
+ **示例:**
141
+
142
+ ```lua
143
+ local mysql = require("mysql")
144
+ if mysql.isConnected() then
145
+ print("数据库已连接")
146
+ else
147
+ print("数据库未连接")
148
+ end
149
+ ```
150
+
151
+ ### 数据操作
152
+
153
+ #### query - 执行查询操作
154
+
155
+ 执行 SELECT 查询操作,用于数据检索。
156
+
157
+ ```lua
158
+ ---@param sql string
159
+ ---@param params any[]?
160
+ ---@return MySQLResult
161
+ function query(sql, params) end
162
+ ```
163
+
164
+ **参数:**
165
+
166
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
167
+ | -------- | ------ | -------- | ------ | ------------------ |
168
+ | `sql` | string | 是 | | SQL 查询语句 |
169
+ | `params` | any[] | 否 | | 查询参数数组,可选 |
170
+
171
+ **返回值:**
172
+
173
+ | 类型 | 描述 |
174
+ | ------------- | ------------ |
175
+ | `MySQLResult` | 查询结果对象 |
176
+
177
+ **示例:**
178
+
179
+ ```lua
180
+ local mysql = require("mysql")
181
+ -- 简单查询
182
+ local result1 = mysql.query("SELECT * FROM users")
183
+ if result1.success then
184
+ print("查询到 " .. tostring(#result1.rows) .. " 条用户记录")
185
+ result1.for _, user in ipairs(rows) do
186
+ print("用户: " .. tostring(user.name) .. ", 邮箱: " .. tostring(user.email))
187
+ end)
188
+ end
189
+ -- 带参数查询
190
+ local result2 = mysql.query("SELECT * FROM users WHERE age > ? AND city = ?", [
191
+ 18,
192
+ "北京",
193
+ ])
194
+ if result2.success then
195
+ print("找到 " .. tostring(#result2.rows) .. " 个符合条件的用户")
196
+ else
197
+ print("查询失败: " .. tostring(result2.error))
198
+ end
199
+ ```
200
+
201
+ #### execute - 执行更新操作
202
+
203
+ 执行 CREATE、INSERT、UPDATE、DELETE 等数据修改操作。
204
+
205
+ ```lua
206
+ ---@param sql string
207
+ ---@param params any[]?
208
+ ---@return MySQLResult
209
+ function execute(sql, params) end
210
+ ```
211
+
212
+ **参数:**
213
+
214
+ | 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
215
+ | -------- | ------ | -------- | ------ | -------------- |
216
+ | `sql` | string | 是 | | SQL 语句 |
217
+ | `params` | any[] | 否 | | 参数数组,可选 |
218
+
219
+ **返回值:**
220
+
221
+ | 类型 | 描述 |
222
+ | ------------- | ------------ |
223
+ | `MySQLResult` | 执行结果对象 |
224
+
225
+ **示例:**
226
+
227
+ ```lua
228
+ local mysql = require("mysql")
229
+ -- 创建表
230
+ local createTableResult = mysql.execute("
231
+ CREATE TABLE IF NOT EXISTS users (
232
+ id INT AUTO_INCREMENT PRIMARY KEY,
233
+ name VARCHAR(255),
234
+ email VARCHAR(255),
235
+ age INT
236
+ )
237
+ ")
238
+ if createTableResult.success then
239
+ print("用户表创建成功")
240
+ else
241
+ print("用户表创建失败: " .. tostring(createTableResult.error))
242
+ end
243
+ -- 插入数据
244
+ local insertResult = mysql.execute(
245
+ "INSERT INTO users (name, email, age) VALUES (?, ?, ?)",
246
+ ["张三", "zhangsan@example.com", 25],
247
+ )
248
+ if insertResult.success then
249
+ print("用户插入成功,ID = " .. tostring(insertResult.insertId))
250
+ print("影响行数: " .. tostring(insertResult.affectedRows))
251
+ end
252
+ -- 更新数据
253
+ local updateResult = mysql.execute("UPDATE users SET age = ? WHERE name = ?", [
254
+ 26,
255
+ "张三",
256
+ ])
257
+ if updateResult.success then
258
+ print("更新了 " .. tostring(updateResult.affectedRows) .. " 条记录")
259
+ end
260
+ -- 删除数据
261
+ local deleteResult = mysql.execute("DELETE FROM users WHERE age < ?", [18])
262
+ if deleteResult.success then
263
+ print("删除了 " .. tostring(deleteResult.affectedRows) .. " 条记录")
264
+ else
265
+ print("删除失败: " .. tostring(deleteResult.error))
266
+ end
267
+ ```
268
+
269
+ ### 事务管理
270
+
271
+ #### beginTransaction - 开始事务
272
+
273
+ ```lua
274
+ ---@return boolean
275
+ function beginTransaction() end
276
+ ```
277
+
278
+ **返回值:**
279
+
280
+ | 类型 | 描述 |
281
+ | --------- | ---------------- |
282
+ | `boolean` | 是否成功开始事务 |
283
+
284
+ **示例:**
285
+
286
+ ```lua
287
+ local mysql = require("mysql")
288
+ if mysql.beginTransaction() then
289
+ print("事务开始")
290
+ else
291
+ print("事务开始失败")
292
+ end
293
+ ```
294
+
295
+ #### commit - 提交事务
296
+
297
+ ```lua
298
+ ---@return boolean
299
+ function commit() end
300
+ ```
301
+
302
+ **返回值:**
303
+
304
+ | 类型 | 描述 |
305
+ | --------- | ---------------- |
306
+ | `boolean` | 是否成功提交事务 |
307
+
308
+ **示例:**
309
+
310
+ ```lua
311
+ local mysql = require("mysql")
312
+ if mysql.commit() then
313
+ print("事务提交成功")
314
+ else
315
+ print("事务提交失败")
316
+ end
317
+ ```
318
+
319
+ #### rollback - 回滚事务
320
+
321
+ ```lua
322
+ ---@return boolean
323
+ function rollback() end
324
+ ```
325
+
326
+ **返回值:**
327
+
328
+ | 类型 | 描述 |
329
+ | --------- | ---------------- |
330
+ | `boolean` | 是否成功回滚事务 |
331
+
332
+ **示例:**
333
+
334
+ ```lua
335
+ local mysql = require("mysql")
336
+ if mysql.rollback() then
337
+ print("事务回滚成功")
338
+ else
339
+ print("事务回滚失败")
340
+ end
341
+ ```
342
+
343
+ ## 完整使用示例
344
+
345
+ ### 基本数据库操作
346
+
347
+ ```lua
348
+ local mysql = require("mysql")
349
+ -- 1. 创建连接配置
350
+ local dbConfig = {
351
+ host = "localhost",
352
+ port = 3306,
353
+ username = "root",
354
+ password = "password",
355
+ database = "my_app",
356
+ charset = "utf8mb4",
357
+ }
358
+ -- 2. 连接数据库
359
+ if mysql.connect(dbConfig) then
360
+ print("数据库连接成功")
361
+ -- 3. 查询数据
362
+ local users = mysql.query("SELECT * FROM users WHERE status = ?", { "active" })
363
+ if users.success then
364
+ print("找到 " .. tostring(#users.rows) .. " 个活跃用户")
365
+ end
366
+ -- 4. 插入新用户
367
+ local newUser = mysql.execute(
368
+ "INSERT INTO users (name, email, status) VALUES (?, ?, ?)",
369
+ { "新用户", "newuser@example.com", "active" },
370
+ )
371
+ if newUser.success then
372
+ print("新用户创建成功,ID = " .. tostring(newUser.insertId))
373
+ end
374
+ -- 5. 断开连接
375
+ mysql.disconnect()
376
+ else
377
+ print("数据库连接失败")
378
+ end
379
+ ```
380
+
381
+ ### 事务处理示例
382
+
383
+ ```lua
384
+ local mysql = require("mysql")
385
+ -- 转账操作示例
386
+ local function transferMoney(fromUserId, toUserId, amount)
387
+ if not mysql.beginTransaction() then
388
+ print("事务开始失败")
389
+ return false
390
+ end
391
+
392
+ local debitResult = mysql.execute(
393
+ "UPDATE accounts SET balance = balance - ? WHERE user_id = ? AND balance >= ?",
394
+ { amount, fromUserId, amount }
395
+ )
396
+ if not debitResult.success or debitResult.affectedRows == 0 then
397
+ mysql.rollback()
398
+ print("余额不足或扣款失败")
399
+ return false
400
+ end
401
+
402
+ local creditResult = mysql.execute(
403
+ "UPDATE accounts SET balance = balance + ? WHERE user_id = ?",
404
+ { amount, toUserId }
405
+ )
406
+ if not creditResult.success or creditResult.affectedRows == 0 then
407
+ mysql.rollback()
408
+ print("转入账户不存在或转账失败")
409
+ return false
410
+ end
411
+
412
+ local logResult = mysql.execute(
413
+ "INSERT INTO transfer_logs (from_user, to_user, amount, created_at) VALUES (?, ?, ?, NOW())",
414
+ { fromUserId, toUserId, amount }
415
+ )
416
+ if not logResult.success then
417
+ mysql.rollback()
418
+ print("转账日志记录失败")
419
+ return false
420
+ end
421
+
422
+ if mysql.commit() then
423
+ print("转账成功: " .. tostring(fromUserId) .. " -> " .. tostring(toUserId) .. ", 金额: " .. tostring(amount))
424
+ return true
425
+ end
426
+ mysql.rollback()
427
+ print("事务提交失败")
428
+ return false
429
+ end
430
+ -- 使用示例
431
+ if mysql.isConnected() then
432
+ local success = transferMoney(1, 2, 100.0)
433
+ if success then
434
+ print("转账操作完成")
435
+ end
436
+ end
437
+ ```
438
+
439
+ ## 注意事项
440
+
441
+ 1. **连接管理**: 使用全局单例连接,确保在应用生命周期内正确管理连接状态
442
+ 2. **参数绑定**: 始终使用参数化查询来防止 SQL 注入攻击
443
+ 3. **事务处理**: 在事务中发生错误时,确保调用 `rollback()` 来回滚事务
444
+ 4. **错误处理**: 检查每个操作的 `success` 字段,并适当处理错误信息
445
+ 5. **资源清理**: 在应用结束时调用 `disconnect()` 来释放数据库连接资源