|
| 1 | +<template> |
| 2 | + <view> |
| 3 | + <page-head :title="title"></page-head> |
| 4 | + <view class="uni-padding-wrap uni-common-mt"> |
| 5 | + <view class="uni-btn-v"><button type="primary" @click="openDB">打开数据库test.db</button></view> |
| 6 | + <view class="uni-btn-v"><button type="primary" @click="executeSQL">创建表database及插入数据</button></view> |
| 7 | + <view class="uni-btn-v"><button type="primary" @click="selectSQL">查询表database的数据</button></view> |
| 8 | + <view class="uni-btn-v"><button type="primary" @click="droptable">删除表database</button></view> |
| 9 | + <view class="uni-btn-v"><button type="primary" @click="closeDB">关闭数据库test.db</button></view> |
| 10 | + <view class="uni-btn-v"><button type="primary" @click="isOpenDB">查询是否打开数据库</button></view> |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + </view> |
| 16 | + </view> |
| 17 | +</template> |
| 18 | + |
| 19 | +<script> |
| 20 | +export default { |
| 21 | + data() { |
| 22 | + return { |
| 23 | + title: 'SQLite' |
| 24 | + }; |
| 25 | + }, |
| 26 | + methods: { |
| 27 | + openDB: function() { |
| 28 | + plus.sqlite.openDatabase({ |
| 29 | + name: 'first', |
| 30 | + path: '_doc/test.db', |
| 31 | + success: function(e) { |
| 32 | + plus.nativeUI.alert('打开数据库test.db: ' + JSON.stringify(e)); |
| 33 | + }, |
| 34 | + fail: function(e) { |
| 35 | + plus.nativeUI.alert('打开数据库test.db: ' + JSON.stringify(e)); |
| 36 | + } |
| 37 | + }); |
| 38 | + }, |
| 39 | + // 执行SQL语句 |
| 40 | + executeSQL: function() { |
| 41 | + plus.sqlite.executeSql({ |
| 42 | + name: 'first', |
| 43 | + sql: 'create table if not exists database("name" CHAR(110),"sex" CHAR(10),"age" INT(11))', |
| 44 | + success: function(e) { |
| 45 | + plus.sqlite.executeSql({ |
| 46 | + name: 'first', |
| 47 | + sql: "insert into database values('彦','女','7000')", |
| 48 | + success: function(e) { |
| 49 | + plus.nativeUI.alert('执行SQL语句: ' + JSON.stringify(e)); |
| 50 | + }, |
| 51 | + fail: function(e) { |
| 52 | + plus.nativeUI.alert('执行SQL语句: ' + JSON.stringify(e)); |
| 53 | + } |
| 54 | + }); |
| 55 | + }, |
| 56 | + fail: function(e) { |
| 57 | + plus.nativeUI.alert('执行SQL语句: ' + JSON.stringify(e)); |
| 58 | + } |
| 59 | + }); |
| 60 | + }, |
| 61 | + // 查询SQL语句 |
| 62 | + selectSQL: function() { |
| 63 | + plus.sqlite.selectSql({ |
| 64 | + name: 'first', |
| 65 | + sql: 'select * from database', |
| 66 | + success: function(e) { |
| 67 | + plus.nativeUI.alert('查询SQL语句: ' + JSON.stringify(e)); |
| 68 | + }, |
| 69 | + fail: function(e) { |
| 70 | + plus.nativeUI.alert('查询SQL语句: ' + JSON.stringify(e)); |
| 71 | + } |
| 72 | + }); |
| 73 | + }, |
| 74 | + // 删除表 |
| 75 | + droptable: function() { |
| 76 | + plus.sqlite.executeSql({ |
| 77 | + name: 'first', |
| 78 | + sql: 'drop table database', |
| 79 | + success: function(e) { |
| 80 | + plus.nativeUI.alert('删除表database: ' + JSON.stringify(e)); |
| 81 | + }, |
| 82 | + fail: function(e) { |
| 83 | + plus.nativeUI.alert('删除表database: ' + JSON.stringify(e)); |
| 84 | + } |
| 85 | + }); |
| 86 | + }, |
| 87 | + // 关闭数据库 |
| 88 | + closeDB: function() { |
| 89 | + plus.sqlite.closeDatabase({ |
| 90 | + name: 'first', |
| 91 | + success: function(e) { |
| 92 | + plus.nativeUI.alert('关闭数据库: ' + JSON.stringify(e)); |
| 93 | + }, |
| 94 | + fail: function(e) { |
| 95 | + plus.nativeUI.alert('关闭数据库: ' + JSON.stringify(e)); |
| 96 | + } |
| 97 | + }); |
| 98 | + }, |
| 99 | + isOpenDB: function() { |
| 100 | + if ( |
| 101 | + plus.sqlite.isOpenDatabase({ |
| 102 | + name: 'first', |
| 103 | + path: '_doc/test.db' |
| 104 | + }) |
| 105 | + ) { |
| 106 | + plus.nativeUI.alert('Opened!'); |
| 107 | + } else { |
| 108 | + plus.nativeUI.alert('Unopened!'); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +}; |
| 113 | +</script> |
| 114 | + |
| 115 | +<style> |
| 116 | + .uni-btn-v { |
| 117 | + margin: 20upx 0; |
| 118 | + padding: 0; |
| 119 | + } |
| 120 | +</style> |
0 commit comments