uniapp下sqlite简易封装
const name = 'gongcheng'
const path = '_doc/local.db'
export const sqlite = {
init(){
return new Promise((resolve, reject) => {
let isOpen = plus.sqlite.isOpenDatabase({name: name,path: path,})
if(!isOpen){
// 打开数据库
plus.sqlite.openDatabase({
name: name,
path: path,
success: function(e){
resolve()
// 新建表
plus.sqlite.executeSql({
name: name,
sql: [
'create table if not exists "工程" ("工程名称" CHAR(110),"工程ID" CHAR(110),"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)',
'create table if not exists "部位" ("工程名称" CHAR(110),"部位名称" CHAR(100),"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)',
'create table if not exists "构件" ("工程名称" CHAR(110),"构件名称" CHAR(100),"部位名称" CHAR(100),"设计标号" CHAR(100),"输送方式" CHAR(100),"检测角度" CHAR(100),"浇筑面" CHAR(100),"碳化深度" CHAR(100),"平均值" CHAR(100),"最小值" CHAR(100),"标准差" CHAR(100),"推定值" CHAR(100),"检测结论" CHAR(100),"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)',
'create table if not exists "测区" ("工程名称" CHAR(110),"构件名称" CHAR(100),"测区值" CHAR(1000),"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)'
],
success: function(e){
console.log('创建成功')
},
fail: function(e){
console.log('executeSql failed: '+JSON.stringify(e));
}
});
},
fail: function(e){
console.log('openDatabase failed: '+JSON.stringify(e));
}
});
}else{
resolve()
}
})
},
/**
* 插入数据
* @param {Object} table 表明
* @param {Object} key
* @param {Object} value
*/
insert(table, key, value){
key = key.map(item => {
return `"${item}"`
}).join(',')
value = value.map(item => {
return `"${item}"`
}).join(',')
let sql = `INSERT INTO "${table}" (${key}) VALUES (${value});`
return this.commonSql(sql);
},
/**
* 查询
* @param {Object} table
* @param {Object} where
*/
select(table, where){
let sql = `SELECT * FROM '${table}' WHERE ${where}`
return new Promise((resolve, reject) => {
plus.sqlite.selectSql({
name: name,
sql:sql,
success(e){
resolve(e)
},
fail(e){
console.log(e);
reject(e)
}
})
})
},
/**
* 更新
* @param {Object} table 表名
* @param {Object} set 更新的值
* @param {Object} where 条件
*/
update(table, set, where){
let sql = `UPDATE '${table}' SET ${set} WHERE ${where}`;
return this.commonSql(sql)
},
/**
* 删除
* @param {Object} table
* @param {Object} where
*/
remove(table, where){
let sql = `DELETE FROM '${table}' WHERE ${where}`
return this.commonSql(sql)
},
/**
* 关闭数据库
*/
close(){
let isOpen = plus.sqlite.isOpenDatabase({name: name,path: path,})
if(!isOpen){
return
}
plus.sqlite.closeDatabase({
name: name,
success(e){
console.log('关闭成功');
},
fail(e){
console.log(e)
}
})
},
commonSql(sql){
console.log('sql' , sql);
return new Promise((resolve, reject) => {
plus.sqlite.executeSql({
name: name,
sql: sql,
success: function(e){
console.log('res',e);
resolve(e)
},
fail: function(e){
console.log('executeSql failed: '+JSON.stringify(e));
reject(e)
}
});
})
}
}
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
上次更新: 2023/09/22, 16:54:32