# mysql-htqw **Repository Path**: htqw123/mysql-htqw ## Basic Information - **Project Name**: mysql-htqw - **Description**: nodejs针对mysql2的封装 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-21 - **Last Updated**: 2024-12-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mysql-htqw #### 简介 针对于nodejs mysql2的二次封装 #### 贡献者 - [htqw](https://github.com/CHETEAM) #### 安装 ```bash npm install mysql-htqw --save ``` #### 实现功能 1. 数据库连接 2. 表操作(创建表、删除表、插入、查询、更新、删除信息) 3. 关闭连接 #### 教程 ```js const mysql = require('mysql-htqw'); (async function main () { const connect = new mysql({ host: 'localhost', // *数据库主机 必选 user: 'test', // 数据库用户名,可选 默认root password: 'test', // 数据库密码,可选 默认无 database: 'test', // *数据库名称 必选 port: 3306 // 数据库端口,可选 默认3306 }) console.log(await connect.table.create( 'test', // *表名 必选 { // 字段名称: '类型' id: 'INT', name: 'TEXT', age: 'INT' } // *字段类型 必选 )) // 创建表 console.log(await connect.table.insert( 'test', // *表名 必选 { // 字段名称: '值' id: '1', name: '"htqw"', age: '18' } // *插入数据 必选 )) // 插入数据 console.log(await connect.table.select( 'test', // *表名 必选 ['id', 'name', 'age'], // *查询字段 必选 'id = 1' // 查询条件 可选 )) // 查询数据 console.log(await connect.table.update( 'test', // *表名 必选 { // 字段名称: '值' name: '"htqw2"', id: '1', age: '19' }, // *更新数据 必选 'id = 1' // 更新条件 可选 )) // 更新数据 console.log(await connect.table.select( 'test', // *表名 必选 ['id', 'name', 'age'], // *查询字段 必选 'id = 1' // 查询条件 可选 )) // 查询数据 console.log(await connect.table.delete( 'test', // *表名 必选 'id = 1' // 删除条件 可选 )) // 删除数据 console.log(await connect.table.drop( 'test' // *表名 必选 )) // 删除表 console.log(await connect.end()) // 关闭连接 })() ``` 1. 使用异步 2. 每个方法都有返回值,返回值是Promise对象 - 当成功时,返回结果 - 当失败时,返回错误信息 #### API - `new mysql(options)` 创建连接 - `options` 数据库配置 - `host` 数据库主机 - `user` 数据库用户名,可选 默认root - `password` 数据库密码,可选 默认无 - `database` 数据库名称 - `port` 数据库端口,可选 默认3306 - `mysql.table` 操作表 - `mysql.table.create(table, cols)` 创建表 - `table` 表名 - `cols` 字段类型 - `字段名称: '类型'` - `mysql.table.drop(table)` 删除表 - `table` 表名 - `mysql.table.insert(table, data)` 插入数据 - `table` 表名 - `data` 插入数据 - `mysql.table.select(table, cols, where)` 查询数据 - `table` 表名 - `cols` 查询字段 - `['字段名称1', '字段名称2', ...]` - `where` 查询条件 - `mysql.table.update(table, data, where)` 更新数据 - `table` 表名 - `data` 更新数据 - `{字段名称: '值', ...}` - `where` 更新条件 - `mysql.table.delete(table, where)` 移除某个信息 - `table` 表名 - `where` 删除条件 - `mysql.end()` 关闭连接 #### 注意 - `mysql.table.create`、`mysql.table.drop`、`mysql.table.insert`、`mysql.table.select`、`mysql.table.update`、`mysql.table.delete`、`mysql.end` 方法都有返回值,返回值是Promise对象 - 当成功时,返回结果 - 当失败时,返回错误信息 #### 联系 - qq: 1931231838 - 电子邮箱: 1931231838@qq.com #### 参与贡献 1. Fork本项目 2. 创建Feat_xxx(功能)_xxx(你的昵称)分支 3. 新建Feat_xxx(功能)_xxx(你的昵称).md文件,描述你的功能 4. 提交代码 5. 新建Pull Request