Blame view

pages/API/sqlite/sqlite.vue 3.11 KB
4b045f7c   刘淇   江阴初始化项目
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
  <template>
  	<view>
  		<page-head :title="title"></page-head>
  		<view class="uni-padding-wrap uni-common-mt">
  			<view class="uni-btn-v"><button type="primary" @click="openDB">打开数据库test.db</button></view>
  			<view class="uni-btn-v"><button type="primary" @click="executeSQL">创建表database及插入数据</button></view>
  			<view class="uni-btn-v"><button type="primary" @click="selectSQL">查询表database的数据</button></view>
  			<view class="uni-btn-v"><button type="primary" @click="droptable">删除表database</button></view>
  			<view class="uni-btn-v"><button type="primary" @click="closeDB">关闭数据库test.db</button></view>
  			<view class="uni-btn-v"><button type="primary" @click="isOpenDB">查询是否打开数据库</button></view>
  			
  			
  			
  			
  		</view>
  	</view>
  </template>
  
  <script>
  export default {
  	data() {
  		return {
  			title: 'SQLite'
  		};
  	},
  	methods: {
  		openDB: function() {
  			plus.sqlite.openDatabase({
  				name: 'first',
  				path: '_doc/test.db',
  				success: function(e) {
  					plus.nativeUI.alert('打开数据库test.db成功 ');
  				},
  				fail: function(e) {
  					plus.nativeUI.alert('打开数据库test.db失败: ' + JSON.stringify(e));
  				}
  			});
  		},
  		// 执行SQL语句
  		executeSQL: function() {
  			plus.sqlite.executeSql({
  				name: 'first',
  				sql: 'create table if not exists database("name" CHAR(110),"sex" CHAR(10),"age" INT(11))',
  				success: function(e) {
  					plus.sqlite.executeSql({
  						name: 'first',
  						sql: "insert into database values('彦','女','7000')",
  						success: function(e) {
  							plus.nativeUI.alert('创建表table和插入数据成功');
  						},
  						fail: function(e) {
  							plus.nativeUI.alert('创建表table成功但插入数据失败: ' + JSON.stringify(e));
  						}
  					});
  				},
  				fail: function(e) {
  					plus.nativeUI.alert('创建表table失败: ' + JSON.stringify(e));
  				}
  			});
  		},
  		// 查询SQL语句
  		selectSQL: function() {
  			plus.sqlite.selectSql({
  				name: 'first',
  				sql: 'select * from database',
  				success: function(e) {
  					plus.nativeUI.alert('查询SQL语句成功: ' + JSON.stringify(e));
  				},
  				fail: function(e) {
  					plus.nativeUI.alert('查询SQL语句失败: ' + JSON.stringify(e));
  				}
  			});
  		},
  		// 删除表
  		droptable: function() {
  			plus.sqlite.executeSql({
  				name: 'first',
  				sql: 'drop table database',
  				success: function(e) {
  					plus.nativeUI.alert('删除表database成功');
  				},
  				fail: function(e) {
  					plus.nativeUI.alert('删除表database失败: ' + JSON.stringify(e));
  				}
  			});
  		},
  		// 关闭数据库
  		closeDB: function() {
  			plus.sqlite.closeDatabase({
  				name: 'first',
  				success: function(e) {
  					plus.nativeUI.alert('关闭数据库成功');
  				},
  				fail: function(e) {
  					plus.nativeUI.alert('关闭数据库失败: ' + JSON.stringify(e));
  				}
  			});
  		},
  		isOpenDB: function() {
  			if (
  				plus.sqlite.isOpenDatabase({
  					name: 'first',
  					path: '_doc/test.db'
  				})
  			) {
  				plus.nativeUI.alert('Opened!');
  			} else {
  				plus.nativeUI.alert('Unopened!');
  			}
  		}
  	}
  };
  </script>
  
  <style>
  	.uni-btn-v {
  		margin: 20rpx 0;
  		padding: 0;
  	}
  </style>