mysql-htqw
v0.1.0
Published
A secondary encapsulation for the nodejs mysql2 library.
Readme
mysql-htqw
简介
针对于nodejs mysql2的二次封装
贡献者
安装
npm install mysql-htqw --save实现功能
- 数据库连接
- 表操作(创建表、删除表、插入、查询、更新、删除信息)
- 关闭连接
教程
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()) // 关闭连接
})()- 使用异步
- 每个方法都有返回值,返回值是Promise对象
- 当成功时,返回结果
- 当失败时,返回错误信息
API
new mysql(options)创建连接options数据库配置host数据库主机user数据库用户名,可选 默认rootpassword数据库密码,可选 默认无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
- 电子邮箱: [email protected]
参与贡献
- Fork本项目
- 创建Feat_xxx(功能)_xxx(你的昵称)分支
- 新建Feat_xxx(功能)_xxx(你的昵称).md文件,描述你的功能
- 提交代码
- 新建Pull Request
