牙叔教程 简单易懂
依赖
"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文档, 最后才是群里问问 --- 牙叔教程
声明
部分内容来自网络 本教程仅用于学习, 禁止用于其他用途