mysql_insert
Insert data into MySQL tables using a structured JSON input. Specify the table name and data array to execute the operation efficiently. Part of the MySQL MCP Server for database management.
Instructions
插入数据到表
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | 要插入的数据数组 | |
| tableName | Yes | 表名称 |
Implementation Reference
- src/server.ts:409-433 (handler)The `handleInsert` method that executes the mysql_insert tool. Constructs parameterized INSERT SQL statements based on the table name and data array, performs batch inserts using dbManager.query, accumulates affected rows, and returns a success message.private async handleInsert(args: { tableName: string; data: any[] }): Promise<any> { if (!Array.isArray(args.data) || args.data.length === 0) { throw new Error('数据必须是非空数组'); } const columns = Object.keys(args.data[0]); const placeholders = columns.map(() => '?').join(', '); const values = args.data.map(row => columns.map(col => row[col])); let affectedRows = 0; for (const rowValues of values) { const sql = `INSERT INTO \`${args.tableName}\` (\`${columns.join('`, `')}\`) VALUES (${placeholders})`; const result = await this.dbManager.query(sql, rowValues); affectedRows += result.affectedRows || 0; } return { content: [ { type: 'text', text: `成功插入 ${affectedRows} 行数据到表 ${args.tableName}`, }, ], }; }
- src/server.ts:155-170 (schema)Input schema definition for the mysql_insert tool in the ListTools response, specifying required tableName (string) and data (array of objects).{ name: 'mysql_insert', description: '插入数据到表', inputSchema: { type: 'object', properties: { tableName: { type: 'string', description: '表名称' }, data: { type: 'array', description: '要插入的数据数组', items: { type: 'object' } }, }, required: ['tableName', 'data'], }, },
- src/server.ts:253-254 (registration)Dispatches calls to the mysql_insert tool to the handleInsert method in the CallToolRequestSchema switch statement.case 'mysql_insert': return await this.handleInsert(args as any);