ThirdView.ets 2.8 KB

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