autojs使用nodejs调用sqlite数据库

本文介绍了如何在AutoJS环境下使用Node.js的Rhino接口,配合SQLite库进行数据库操作,包括创建数据库和表,以及插入和查询数据的方法。示例代码详细展示了创建数据库、异步插入多条数据和查询指定数据的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

牙叔教程 简单易懂

依赖

"nodejs";
require("rhino").install();
const { device } = require("device");
const path = require("path");
const fs = require("fs");
const util = require("util");
const SQLiteDatabase = android.database.sqlite.SQLiteDatabase;

创建数据库

let dir = device.externalStorageDirectory; // /storage/emulated/0
let dbPath = path.join(dir, "bullet_comment.db"); // /storage/emulated/0/bullet_comment.db
let db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);

创建表

async function createTable(db) {
  let CREATE_Table = `CREATE TABLE IF NOT EXISTS dou_yin_bullet_comment_table(
  'id' INTEGER PRIMARY KEY AUTOINCREMENT,
  'name' TEXT NOT NULL,
  'comment' TEXT,
  'level' INTEGER,
  'complete_comment' TEXT UNIQUE,
  'ts' TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) `;
  db.execSQL(CREATE_Table); //创建用户表
}

增加一条数据

function insertItem(db, item) {
  let sql =
    "INSERT OR REPLACE INTO dou_yin_bullet_comment_table (name, comment, level, complete_comment) VALUES (?, ?, ?, ?)";
  db.beginTransaction();
  db.execSQL(sql, [item.name, item.comment, item.level, item.complete_comment]);
  db.setTransactionSuccessful();
  db.endTransaction();
}

增加多条数据

async function insertItems(db, items) {
  for (let i = 0; i < items.length; i++) {
    console.log("i = " + i);
    insertItem(db, items[i]);
    await sleep(1000);
  }
}

增加多条数据2

async function insertItemsByStatement(db, items) {
  let sql =
    "INSERT OR REPLACE INTO dou_yin_bullet_comment_table (name, comment, level, complete_comment) VALUES (?, ?, ?, ?)";
  db.beginTransaction();
  let statement = db.compileStatement(sql);
  for (let i = 0; i < items.length; i++) {
    let item = items[i];
    statement.bindString(1, item.name);
    statement.bindString(2, item.comment);
    statement.bindLong(3, item.level);
    statement.bindString(4, item.complete_comment);
    statement.execute();
    statement.clearBindings();
  }

  db.setTransactionSuccessful(); //设置事务处理成功,不设置会自动回滚不提交。
  db.endTransaction();
}

查询所有数据

function queryItems(db) {
  let query = "SELECT * FROM dou_yin_bullet_comment_table";
  let cursor = db.rawQuery(query, null);
  cursor.moveToFirst();
  let bulletComments = [];
  while (!cursor.isAfterLast()) {
    let bulletComment = {
      id: cursor.getInt(0),
      name: cursor.getString(1),
      comment: cursor.getString(2),
      level: cursor.getInt(3),
      complete_comment: cursor.getString(4),
      ts: cursor.getString(5),
    };
    bulletComments.push(bulletComment);
    cursor.moveToNext();
  }
  cursor.close();
  return bulletComments;
}

查询指定数据

function queryItemsByTs(db, count) {
  let sql = "SELECT * FROM dou_yin_bullet_comment_table ORDER BY ts DESC LIMIT " + count;
  db.beginTransaction();
  let cursor = db.rawQuery(sql, null);
  cursor.moveToFirst();
  let bulletComments = [];
  while (!cursor.isAfterLast()) {
    let bulletComment = {
      id: cursor.getInt(0),
      name: cursor.getString(1),
      comment: cursor.getString(2),
      level: cursor.getInt(3),
      complete_comment: cursor.getString(4),
      ts: cursor.getString(5),
    };
    bulletComments.push(bulletComment);
    cursor.moveToNext();
  }
  cursor.close();
  return bulletComments;
}

环境

设备: 小米11pro
Android版本: 12
Autojs版本: 9.3.11

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问 --- 牙叔教程

声明

部分内容来自网络 本教程仅用于学习, 禁止用于其他用途

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牙叔教程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值