forked from MisterZakary/-LazyStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateTikuNet.js
75 lines (69 loc) · 2.15 KB
/
updateTikuNet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 加载jsoup.jar
runtime.loadJar("./jsoup-1.12.1.jar");
// 使用jsoup解析html
importClass(org.jsoup.Jsoup);
importClass(org.jsoup.nodes.Document);
//importClass(org.jsoup.nodes.Element);
importClass(org.jsoup.select.Elements);
importClass(android.database.sqlite.SQLiteDatabase);
/**
* 插入tikuNet表
* @param {} liArray li列表,包含题目和答案
*/
function CreateAndInsert(liArray){
var dbName = "tiku.db";
//文件路径
var path = files.path(dbName);
//确保文件存在
if (!files.exists(path)) {
files.createWithDirs(path);
}
//创建或打开数据库
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
var createTable = "\
CREATE TABLE IF NOt EXISTS tikuNet(\
question CHAR(253),\
answer CHAR(100)\
);";
var cleanTable = "DELETE FROM tikuNet";
db.execSQL(createTable);
db.execSQL(cleanTable);
log("创建打开清空表tikuNet!");
var sql = "INSERT INTO tikuNet (question, answer) VALUES (?, ?)";
db.beginTransaction();
var stmt = db.compileStatement(sql);
for (var li = 0, len = liArray.size(); li < len; li++) {
//log("题目:"+li.text());
var liText = liArray.get(li).text();
var timuPos=liText.indexOf("】")+1;
var tiMu=liText.substring(timuPos).replace("_", "");
var daAn = liArray.get(li).select("b").first().text();
log(util.format("题目:%s\n答案:%s"),tiMu,daAn);
stmt.bindString(1, tiMu);
stmt.bindString(2, daAn);
stmt.executeInsert();
stmt.clearBindings();
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
return true;
}
/**
*/
function updateTikunet() {
log("开始更新数据库...");
var htmlString = Jsoup.connect("http://49.235.90.76:5000").maxBodySize(0).timeout(10000).get();
var htmlArray = Jsoup.parse(htmlString);
var liArray = htmlArray.select("li:has(b)");
log(util.format("题库数目%s\n"), liArray.size());
//执行更新
if(CreateAndInsert(liArray))
{
return liArray.size();
}else{
return -1;
}
}
//updateTikunet();
module.exports = updateTikunet;