ThirdView.ets 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Pet, RDBMapper, RelationalStoreUtils, Test } from "basic";
  2. @Component
  3. export struct ThirdView {
  4. @State list: Test[] = []
  5. @State list2: Pet[] = []
  6. id1:number = 0
  7. id2:number = 0
  8. petMapper:RDBMapper<Pet> = new RDBMapper(Pet)
  9. build() {
  10. Column() {
  11. Row() {
  12. Button('测试新增')
  13. .onClick(() => {
  14. RelationalStoreUtils.insert(Test, { name: 'test1', age: 18 }, (id) => {
  15. console.log('insert success');
  16. this.id1 = id
  17. let list = RelationalStoreUtils.getListSync(Test)
  18. this.list = list
  19. console.log('list:', JSON.stringify(list))
  20. })
  21. })
  22. Button('测试修改')
  23. .onClick(() => {
  24. RelationalStoreUtils.updateItemById(Test, { id: this.id1, name: '测试1修改', age: 20 }, () => {
  25. console.log('insert success');
  26. let list = RelationalStoreUtils.getListSync(Test)
  27. this.list = list
  28. console.log('list:', JSON.stringify(list))
  29. })
  30. })
  31. Button('测试删除')
  32. .onClick(() => {
  33. RelationalStoreUtils.deleteItemById(Test, this.id1)
  34. console.log('insert success');
  35. let list = RelationalStoreUtils.getListSync(Test)
  36. this.list = list
  37. console.log('list:', JSON.stringify(list))
  38. })
  39. }
  40. .width('100%')
  41. .justifyContent(FlexAlign.SpaceAround)
  42. .margin({ bottom: 30 })
  43. Text(JSON.stringify(this.list))
  44. .font({ size: 20 })
  45. .margin({ bottom: 20 })
  46. Row() {
  47. Button('测试新增2')
  48. .onClick(() => {
  49. this.petMapper.insert( { name: 'test1', tag: '标签' }, (id:number) => {
  50. console.log('insert success');
  51. this.id2 = id
  52. let list = this.petMapper.getListSync()
  53. this.list2 = list
  54. console.log('list:', JSON.stringify(list))
  55. })
  56. })
  57. Button('测试修改2')
  58. .onClick(() => {
  59. this.petMapper.updateItemById({ id: this.id2, name: '测试1修改',tag: '标签2' }, () => {
  60. console.log('insert success');
  61. let list = this.petMapper.getListSync()
  62. this.list2= list
  63. console.log('list:', JSON.stringify(list))
  64. })
  65. })
  66. Button('测试删除2')
  67. .onClick(() => {
  68. this.petMapper.deleteItemByIdSync(this.id2)
  69. console.log('insert success');
  70. let list = this.petMapper.getListSync()
  71. this.list2 = list
  72. console.log('list:', JSON.stringify(list))
  73. })
  74. }
  75. .width('100%')
  76. .justifyContent(FlexAlign.SpaceAround)
  77. .margin({ bottom: 30 })
  78. Text(JSON.stringify(this.list2))
  79. .font({ size: 20 })
  80. .margin({ bottom: 20 })
  81. }
  82. .justifyContent(FlexAlign.Center)
  83. .alignItems(HorizontalAlign.Center)
  84. .height('100%')
  85. .width('100%')
  86. }
  87. }