This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
tikuCommon.js
86 lines (70 loc) · 1.99 KB
/
tikuCommon.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
75
76
77
78
79
80
81
importClass(android.database.sqlite.SQLiteDatabase);
function searchTiku(keyw) {
//表名
var tableName = "tiku";
var ansArray = searchDb(keyw, tableName, "");
return ansArray;
}
function searchDb(keyw, _tableName, queryStr) {
var tableName = _tableName;
//数据文件名
var dbName = "tiku.db";
//文件路径
var path = files.path(dbName);
//确保文件存在
if (!files.exists(path)) {
files.createWithDirs(path);
}
//创建或打开数据库
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
var query = "";
if (queryStr == "") {
query = "SELECT question,answer FROM " + tableName + " WHERE question LIKE '" + keyw + "%'";//前缀匹配
} else {
query = queryStr;
}
log(query);
//query="select * from tiku"
//db.execSQL(query);
var cursor = db.rawQuery(query, null);
cursor.moveToFirst();
var ansTiku = [];
if (cursor.getCount() > 0) {
do {
var timuObj={"question" : cursor.getString(0),"answer":cursor.getString(1)};
ansTiku.push(timuObj);
} while (cursor.moveToNext());
} else {
log("题库中未找到: " + keyw);
}
cursor.close();
return ansTiku;
}
function executeSQL(sqlstr) {
//数据文件名
var dbName = "tiku.db";
//文件路径
var path = files.path(dbName);
//确保文件存在
if (!files.exists(path)) {
files.createWithDirs(path);
}
//创建或打开数据库
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
db.execSQL(sqlstr);
toastLog(sqlstr);
db.close();
}
function indexFromChar(str) {
return str.charCodeAt(0) - "A".charCodeAt(0);
}
function searchNet(keyw) {
var tableName = "tikuNet";
var ansArray = searchDb(keyw, tableName, "");
return ansArray;
}
exports.searchTiku = searchTiku;
exports.searchNet = searchNet;
exports.searchDb = searchDb;
exports.indexFromChar = indexFromChar;
exports.executeSQL = executeSQL;