sqlite.vue 3.1 KB

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