test.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view>
  3. 默认打开数据库,并创建表
  4. <button type="primary" @click="openSqlite">打开数据库</button>
  5. <button type="primary" @click="createTable">创建表</button>
  6. <button type="primary" @click="closedb">关闭数据库</button>
  7. <button type="primary" @click="addProp">给表加字段</button>
  8. <button type="primary" @click="addData">添加数据</button>
  9. <button type="primary" @click="addListData">批量添加数据</button>
  10. <button type="primary" @click="changeListData">批量修改数据</button>
  11. <button type="primary" @click="getData">查询第一页数据</button>
  12. <button type="primary" @click="changeData">修改最后一条数据</button>
  13. <button type="primary" @click="clearData">清空数据</button>
  14. <button type="primary" @click="deleteTable">删除数据表</button>
  15. <view>总页数:{{pages}} 总数据条数:{{total}}</view>
  16. <view></view>
  17. <view v-for="(item,index) in list" :key="index">
  18. <span>fid:{{item.fid}},{{item.title}},</span>
  19. <span>{{item.content}},</span>
  20. <span>{{item.desc}}</span>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import { openSqlite,executeSql,closedb,getTable,isTable,getAllField,insertAll,addSql,getPageList,
  26. selectList,deleteSql,updateSql,selectSql,batchUpdate} from "@/utils/database";
  27. export default {
  28. data(){
  29. return {
  30. tableName:"ord_storage_order",
  31. options:{
  32. current:1, // 分页默认为第1页
  33. size:10,
  34. },
  35. list:[],
  36. pages:0,
  37. total:0,
  38. }
  39. },
  40. onUnload(){
  41. // 关闭数据库
  42. this.closedb()
  43. },
  44. onLoad(){
  45. const dbPath = plus.io.convertLocalFileSystemURL('_doc/fr_storage.db');
  46. console.log(dbPath); // 输出类似"/storage/emulated/0/Android/data/com.example.myapp/files/database/test.db"的路径
  47. },
  48. async onShow(){
  49. /**
  50. * 数据库设置名称 在 database.js 文件中
  51. */
  52. // console.log("所有表名称",await getTable())
  53. // 默认打开数据库,并创建表
  54. await this.openSqlite()
  55. await this.createTable()
  56. },
  57. methods: {
  58. async deleteTable(){
  59. try{
  60. await executeSql("drop table ord_storage_order")
  61. uni.showToast({
  62. title:"删除数据表成功",
  63. icon:"none"
  64. })
  65. }catch(e){
  66. uni.showToast({
  67. title:"删除数据表失败",
  68. icon:"none"
  69. })
  70. console.error("删除数据表失败ord_storage_order",e)
  71. }
  72. },
  73. // 创建表
  74. async createTable(){
  75. let sql = this.createTableSql_outbound()
  76. try{
  77. let exist = await isTable(this.tableName)
  78. console.log("表是否存在",exist)
  79. if(!exist){
  80. let res = await executeSql(sql)
  81. uni.showToast({
  82. title:"新增数据表成功",
  83. icon:"none"
  84. })
  85. console.log("新增表ord_storage_order",res)
  86. }else{
  87. // uni.showToast({
  88. // title:"数据表已存在",
  89. // icon:"none"
  90. // })
  91. }
  92. }catch(e){
  93. uni.showToast({
  94. title:"新增数据表失败",
  95. icon:"none"
  96. })
  97. console.error("新增表报错ord_storage_order",e)
  98. }
  99. },
  100. // 更新表字段
  101. async addProp(){
  102. // 更新app时,可以增加字段
  103. try{
  104. let res = await executeSql(`ALTER TABLE ord_storage_order ADD desc varchar(500) DEFAULT '1'`)
  105. console.log("增加字段",res)
  106. uni.showToast({
  107. title:"新增字段成功",
  108. icon:"none"
  109. })
  110. }catch(e){
  111. uni.showToast({
  112. title:"字段已存在,或者未打开数据库,或者数据表不存在",
  113. icon:"none"
  114. })
  115. }
  116. },
  117. // 关闭数据库
  118. closedb(){
  119. try{
  120. closedb()
  121. }catch(e){
  122. console.error("关闭数据库,报错",e)
  123. }
  124. },
  125. async openSqlite(){
  126. // 打开数据库
  127. try{
  128. let b = await openSqlite()
  129. uni.showToast({
  130. title:"打开数据库成功",
  131. icon:"none"
  132. })
  133. }catch(e){
  134. console.error("打开数据库,报错",e)
  135. }
  136. },
  137. // 添加数据
  138. async addData(){
  139. try{
  140. let b = await addSql(this.tableName,{title:"你好,世界!",content:"世界:你好呀!",desc:"嘻嘻"})
  141. uni.showToast({
  142. title:"添加成功",
  143. icon:"none"
  144. });
  145. }catch(e){
  146. console.error("添加数据,报错",e)
  147. }
  148. },
  149. // 批量插入数据
  150. async addListData(){
  151. try{
  152. let list = []
  153. for(let i=0;i<3;i++){
  154. list.push({title:"你好,世界!",content:"世界:你好呀!",desc:"嘻嘻"})
  155. }
  156. list.push({title:"你好,世界!"})
  157. list.push({title:"你好,世界!",content:"世界:你好呀!",s:1})
  158. await insertAll(this.tableName,list)
  159. uni.showToast({
  160. title:"添加成功",
  161. icon:"none"
  162. });
  163. }catch(e){
  164. console.error("批量添加数据,报错",e)
  165. }
  166. },
  167. // 批量修改数据
  168. async changeListData(){
  169. // 查询最后10条数据,并修改
  170. let res = await getPageList(this.tableName,{current:1,size:10},{},"fid desc")
  171. let list = res.data.data.records
  172. for(let i in list){
  173. list[i].title = "批量修改"
  174. }
  175. console.log("list",list)
  176. try{
  177. await batchUpdate(this.tableName,list,"fid")
  178. // uni.showToast({
  179. // title:"修改成功",
  180. // icon:"none"
  181. // });
  182. }catch(e){
  183. console.error("批量修改数据,报错",e)
  184. }
  185. },
  186. // 获取数据
  187. async getData(){
  188. try{
  189. let where = {}
  190. let res = await getPageList(this.tableName,this.options,where,"fid desc")
  191. console.log("加载数据",res)
  192. this.list = res.data.data.records
  193. this.pages = res.data.data.pages
  194. this.total = res.data.data.total
  195. }catch(e){
  196. uni.showToast({
  197. title:"报错,请查看控制台",
  198. icon:"none"
  199. });
  200. console.error("报错",e)
  201. }
  202. },
  203. // 修改数据
  204. async changeData(){
  205. // fid 是自增id
  206. try{
  207. let res = await getPageList(this.tableName,{current:1,size:1},{},"fid desc")
  208. console.log("最后一页数据",res.data.data)
  209. await updateSql(this.tableName,{title:"修改了数据"},{fid:res.data.data.records[0].fid})
  210. uni.showToast({
  211. title:"修改成功",
  212. icon:"none"
  213. });
  214. }catch(e){
  215. uni.showToast({
  216. title:"修改报错,请查看控制台",
  217. icon:"none"
  218. });
  219. console.error("修改报错",e)
  220. }
  221. },
  222. async clearData(){
  223. try{
  224. await executeSql("DELETE FROM ord_storage_order")
  225. uni.showToast({
  226. title:"清除数据成功",
  227. icon:"none"
  228. });
  229. }catch(e){
  230. uni.showToast({
  231. title:"清除数据报错,请查看控制台",
  232. icon:"none"
  233. });
  234. console.error("清除数据报错",e)
  235. }
  236. },
  237. /**
  238. * 创建表, 仅供参考
  239. * @returns {string}
  240. * 因为java后台用的id,所以这里用fid 作为自增id
  241. */
  242. createTableSql_outbound(){
  243. return "CREATE TABLE IF NOT EXISTS `ord_storage_order` (" +
  244. " `id` int(11) DEFAULT '' ," +
  245. " `fid` INTEGER PRIMARY KEY AUTOINCREMENT," +
  246. " `title` varchar(50) DEFAULT NULL ," +
  247. " `content` varchar(50) DEFAULT NULL ," +
  248. " `updateId` int(11) DEFAULT NULL ," +
  249. " `updateName` varchar(50) DEFAULT NULL ," +
  250. " `checkStatus` int(11) DEFAULT '0' ," +
  251. " `status` int(11) DEFAULT '0' ," +
  252. " `createTime` datetime DEFAULT CURRENT_TIMESTAMP ," +
  253. " `updateTime` datetime DEFAULT NULL default(datetime('now','localtime')) ," +
  254. " `deleted` char(1) DEFAULT '0' ," +
  255. " `tenantId` int(11) NOT NULL DEFAULT '0' ," +
  256. " `handleOption` varchar(500) DEFAULT NULL" +
  257. "); "
  258. },
  259. }
  260. }
  261. </script>
  262. <style lang='scss' scoped>
  263. button{
  264. margin-bottom:5px;
  265. }
  266. </style>