Node.js+SQLite后端数据库的读写与操作示例

SQLite数据的封装体

var sqlite3 = require('sqlite3').verbose();

// sqlites数据库地址
var path = require('path');
var sqliteDbPath = path.resolve(__dirname, './sqlite3.db');

// 打开sqlites数据库
var sqlite3Connection = new sqlite3.Database(sqliteDbPath, (err: any) => {
  if (err) throw err;
});

type Istrings = string | string[] | undefined;
interface Idb {
  type: string;
  table: string;
  field?: Istrings;
  value?: Istrings;
  set?: Istrings;
  where?: Istrings;
  order?: Istrings;
  limit?: number;
}

class DbSQLite3 {
  pool: any;
  constructor(pool: any) {
    this.pool = pool;
  }
  joinSql(sql: Idb, type: 'where' | 'order' | 'value' | 'set' | 'field') {
    var val = sql[type];
    if (Array.isArray(val)) {
      if (val.length) {
        if (type === 'where') {
          return ' where ' + val.join(' and ');
        } else if (type === 'order') {
          return ' order by ' + val.join(',');
        } else if (type === 'value') {
          return ' values (' + val.join('),(') + ')';
        } else if (type === 'set') {
          return ' set ' + val.join(',');
        } else if (type === 'field') {
          return ' (' + val.join(',') + ')';
        }
      } else {
        if (['value', 'set'].includes(type)) {
          // 不能为空
        }
      }
    } else if (val) {
      if (type === 'where') {
        return ' where ' + val;
      } else if (type === 'order') {
        return ' order by ' + val;
      } else if (type === 'value') {
        return ' values (' + val + ')';
      } else if (type === 'set') {
        return ' set ' + val;
      } else if (type === 'field') {
        return ' (' + val + ')';
      }
    }
    return '';
  }
  makeSql(sql: Idb) {
    // sqlite查询sql的时候,有些指定的语句不支持order与limit
    var sqls = sql.type;
    if (sqls === 'insert') {
      sqls += ' into';
      sqls += ' ' + sql.table;
      sqls += this.joinSql(sql, 'field');
      sqls += this.joinSql(sql, 'value');
    } else if (sqls === 'delete') {
      sqls += ' from ' + sql.table;
      sqls += this.joinSql(sql, 'where');
    } else if (sqls === 'update') {
      sqls += ' ' + sql.table;
      sqls += this.joinSql(sql, 'set');
      sqls += this.joinSql(sql, 'where');
    } else if (sqls === 'select') {
      sqls += sql.field ? ' ' + sql.field : ' *';
      sqls += ' from ' + sql.table;
      sqls += this.joinSql(sql, 'where');
      sqls += this.joinSql(sql, 'order');
      sqls += sql.limit ? ' limit ' + sql.limit : '';
    }
    console.log(666.111, sqls);
    return sqls;
  }
  getErr(err: any) {
    var errs = err?.message || null;
    return errs;
  }
  getResult(rows: any) {
    var result = rows ?? 'OK';
    return result;
  }
  query(sql: Idb, callback: any) {
    console.log(666.101, sql, callback);
    if (sql.type === 'select') {
      if (sql.where && (!sql.limit || sql.limit === 1) && false) {
        // 直接返回一个obj
        this.pool.each(this.makeSql(sql), (err: any, rows: any) => {
          console.log(666.103, err, rows);
          callback(this.getErr(err), this.getResult(rows));
        });
      } else {
        this.pool.all(this.makeSql(sql), (err: any, rows: any) => {
          console.log(666.104, err, rows);
          callback(this.getErr(err), this.getResult(rows));
        });
      }
    } else {
      this.pool.run(this.makeSql(sql), (err: any, rows: any) => {
        console.log(666.105, err, rows);
        callback(this.getErr(err), this.getResult(rows));
      });
    }
  }
}
export default new DbSQLite3(sqlite3Connection);

读取示例,与上一篇的mysql保持一致

import db from './db/sqlite/db';

// 假设我们有个数据表users,有id、userName、userPassword、userType四个字段
var a = 'select';
// a = 'insert';
if (a === 'delete') {
  db.query(
    {
      type: 'delete',
      table: 'user',
      where: 'userName="SSSS"',
      limit: 2,
      order: 'id desc',
    },
    (err: any, result: any) => {
      if (err) {
        console.log(666.201, err);
      } else {
        console.log(666.202, result);
        if (result) {
          console.log(666.2022, '删除结果:' + result);
        }
      }
    }
  );
} else if (a === 'update') {
  db.query(
    {
      type: 'update',
      table: 'user',
      // set: 'userName="GGGG"',
      set: ['userName="SSSS"', 'userPassword="FFFF"'],
      where: 'id=10',
      limit: 3,
      order: 'id desc',
    },
    (err: any, result: any) => {
      if (err) {
        console.log(666.201, err);
      } else {
        console.log(666.202, result);
        if (result) {
          console.log(666.2022, '修改结果:' + result);
        }
      }
    }
  );
} else if (a === 'insert') {
  db.query(
    {
      type: 'insert',
      table: 'user',
      field: 'userName,userPassword,userType',
      // value: '"asai","123456",8',
      value: ['"asai","123456",8', '"阿萨","分sss",88', '"阿萨","分sss",88'],
    },
    (err: any, result: any) => {
      if (err) {
        console.log(666.201, err);
      } else {
        console.log(666.202, result);
        if (result) {
          console.log(666.2022, '增加结果:' + result);
        }
      }
    }
  );
} else if (a === 'select') {
  db.query(
    {
      type: 'select',
      table: 'user',
      field: '*',
      where: 'id>5',
      limit: 1,
      order: 'id desc',
    },
    (err: any, result: any) => {
      if (err) {
        console.log(666.201, err);
      } else {
        console.log(666.202, result?.length, result);
        if (result && result.length) {
          result.forEach((element: any) => {
            console.log(666.2022, element?.id, element);
          });
        }
      }
    }
  );
}

你可能感兴趣的:(mysql,SQLite,nodejs,数据库,sqlite,node.js)