ThirdView.ets 3.0 KB

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