Studio android mongodb

Realm Database là một dịch vụ được cung cấp bởi MongoDb, được sử dụng để lưu trữ cục bộ dữ liệu trong thiết bị của người dùng. Với sự trợ giúp của dữ liệu này có thể được lưu trữ dễ dàng trong thiết bị của người dùng và có thể được truy cập dễ dàng. Chúng tôi có thể sử dụng cơ sở dữ liệu này để lưu trữ dữ liệu trong chính thiết bị của người dùng. Đây là một loạt 4 bài viết trong đó chúng ta sẽ thực hiện thao tác CRUD (Tạo, Đọc, Cập nhật và Xóa) cơ bản với Cơ sở dữ liệu Realm trong Android. Chúng tôi sẽ đề cập đến 4 bài viết sau trong loạt bài này

  1. Làm cách nào để cài đặt và thêm dữ liệu vào cơ sở dữ liệu Realm trong Android?
  2. Làm cách nào để đọc dữ liệu từ cơ sở dữ liệu Realm trong Android?
  3. Làm cách nào để cập nhật dữ liệu vào cơ sở dữ liệu Realm trong Android?
  4. Làm cách nào để xóa dữ liệu trong cơ sở dữ liệu Realm trong Android?

Trong bài viết này, chúng ta sẽ xem xét cài đặt và thêm dữ liệu vào Realm Database trong Android.  

Dữ liệu đang được lưu trữ trong cơ sở dữ liệu Realm như thế nào?

Dữ liệu được lưu trữ trong cơ sở dữ liệu Realm dưới dạng bảng. Khi chúng tôi lưu trữ dữ liệu này trong cơ sở dữ liệu Realm, nó được sắp xếp ở dạng bảng tương tự như bảng Excel. Dưới đây là đại diện của cơ sở dữ liệu Realm mà chúng tôi đang lưu trữ trong cơ sở dữ liệu Realm của mình.  

Studio android mongodb

Những gì chúng ta sẽ xây dựng trong bài viết này?

Chúng tôi sẽ xây dựng một ứng dụng đơn giản trong đó chúng tôi sẽ thêm dữ liệu vào cơ sở dữ liệu Realm. Chúng tôi sẽ tạo cơ sở dữ liệu để thêm tên khóa học, mô tả khóa học và thời lượng khóa học. Chúng tôi sẽ lưu tất cả dữ liệu này trong cơ sở dữ liệu Realm của chúng tôi. Một video mẫu được cung cấp bên dưới để có ý tưởng về những gì chúng ta sẽ làm trong bài viết này. Lưu ý rằng chúng tôi sẽ triển khai dự án này bằng ngôn ngữ Java.  

https. //phương tiện truyền thông. chuyên viên máy tính. org/wp-content/uploads/20210319214045/20210319_213900. mp4

Từng bước thực hiện

Bước 1. Tạo một dự án mới

Để tạo dự án mới trong Android Studio, vui lòng tham khảo Cách tạo/Bắt đầu dự án mới trong Android Studio. Lưu ý chọn Java làm ngôn ngữ lập trình

Bước 2. Thêm phụ thuộc vào phần phụ thuộc trong bản dựng cấp dự án. tập tin lớp

Điều hướng đến ứng dụng> Gradle Script> build. gradle (Dự án) và thêm phụ thuộc đường dẫn lớp trong phần phụ thuộc. Bạn có thể thấy sự phụ thuộc trong phần bên dưới.  

dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        
        // add below dependency
        classpath "io.realm:realm-gradle-plugin:10.3.1"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
}

Sau khi thêm cái này bây giờ điều hướng để xây dựng. gradle (Module) và thêm đoạn mã dưới đây vào nó. Thêm plugin vào đầu tập tin này.  

apply plugin: 'realm-android'     

Sau đó, thêm đoạn mã dưới đây vào phần phụ thuộc.  

realm {
    syncEnabled = true
}

Bây giờ hãy đồng bộ hóa dự án của bạn và bây giờ chúng ta sẽ chuyển sang tạo một lớp java mới. Dưới đây là mã hoàn chỉnh cho bản dựng. tệp lớp (Mô-đun)

Java




plugins {

    id'com.android.application'

}

 

apply plugin:'realm-android'

 

apply plugin: 'realm-android'     
0

    

apply plugin: 'realm-android'     
2
apply plugin: 'realm-android'     
3

    

apply plugin: 'realm-android'     
5_______1_______6

    

apply plugin: 'realm-android'     
8
apply plugin: 'realm-android'     
9

 

    

realm {
    syncEnabled = true
}
1

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
3
realm {
    syncEnabled = true
}
4

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
6
realm {
    syncEnabled = true
}
7

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
9
apply plugin: 'realm-android'     
3

realm {
    syncEnabled = true
}
2plugins {2plugins {3

realm {
    syncEnabled = true
}
2plugins {5plugins {6

 

realm {
    syncEnabled = true
}
2plugins {8plugins {9

    }

 

        3

realm {
    syncEnabled = true
}
2    5

    6    7    8

    6id0id1id2id3

realm {
    syncEnabled = true
}
2}

    }

    id9

realm {
    syncEnabled = true
}
2'com.android.application'1

realm {
    syncEnabled = true
}
2'com.android.application'3

    }

}

 

'com.android.application'7

    'com.android.application'9____332_______0

}

 

}2

    }4}5

    }4}8

    }4apply plugin:1

    apply plugin:3_______333_______4

    apply plugin:6_______333_______7

    apply plugin:6_______334_______0

}

Bước 3. Tạo một lớp java mới để khởi tạo realmeDatabase

Điều hướng đến ứng dụng > java > tên gói ứng dụng của bạn > Nhấp chuột phải vào nó > Mới > Lớp Java và đặt tên là RealmDb và thêm mã bên dưới vào đó.  

Java




'realm-android'2 'realm-android'3

 

'realm-android'2 'realm-android'5

'realm-android'2 'realm-android'7

 

'realm-android'8 'realm-android'9

apply plugin: 'realm-android'     
00
apply plugin: 'realm-android'     
01
apply plugin: 'realm-android'     
02

    

apply plugin: 'realm-android'     
04

    'realm-android'8

apply plugin: 'realm-android'     
07
apply plugin: 'realm-android'     
08

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
10
apply plugin: 'realm-android'     
11

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
14

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
16

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
18
apply plugin: 'realm-android'     
19
apply plugin: 'realm-android'     
20

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
23

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
25

apply plugin: 'realm-android'     
26_______1_______27
apply plugin: 'realm-android'     
28

apply plugin: 'realm-android'     
29_______1_______30

apply plugin: 'realm-android'     
29_______1_______32

apply plugin: 'realm-android'     
29_______1_______34}0
apply plugin: 'realm-android'     
36

apply plugin: 'realm-android'     
29_______1_______38

apply plugin: 'realm-android'     
29_______1_______40

apply plugin: 'realm-android'     
29_______1_______42

apply plugin: 'realm-android'     
29_______1_______44

apply plugin: 'realm-android'     
29_______1_______46

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
48

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
50

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
52

    }

}

Bước 4. Định nghĩa lớp này trong AndroidManifest. tập tin xml

Navigate to the app > AndroidManifest.xml file and inside the tag add the below line. 

Android. tên =”. RealmDb”

Bây giờ chúng ta sẽ chuyển sang làm việc với Activity_main. xml.  

Bước 5. Làm việc với Activity_main. tập tin xml

Điều hướng đến ứng dụng > độ phân giải > bố cục > Activity_main. xml và thêm mã bên dưới vào tệp đó. Dưới đây là mã cho Activity_main. tập tin xml.  

XML




apply plugin: 'realm-android'     
56
apply plugin: 'realm-android'     
57
apply plugin: 'realm-android'     
58
apply plugin: 'realm-android'     
59plugins {6
apply plugin: 'realm-android'     
61
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
63
apply plugin: 'realm-android'     
64

apply plugin: 'realm-android'     
65
apply plugin: 'realm-android'     
66

    

apply plugin: 'realm-android'     
68
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
70

    

apply plugin: 'realm-android'     
72
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
74

    

apply plugin: 'realm-android'     
76
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
78

    

apply plugin: 'realm-android'     
80_______1_______59
apply plugin: 'realm-android'     
78

    

apply plugin: 'realm-android'     
84_______1_______59
apply plugin: 'realm-android'     
86

    

apply plugin: 'realm-android'     
88
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
90_______1_______91

 

    

apply plugin: 'realm-android'     
93

    

apply plugin: 'realm-android'     
65_______1_______96

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
98
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
00

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
76
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
78

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
80
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
08

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
10
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
12

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
14
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
16
realm {
    syncEnabled = true
}
17

 

    

realm {
    syncEnabled = true
}
19

    

apply plugin: 'realm-android'     
65_______1_______96

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
98
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
26

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
76
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
78

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
80
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
08

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
10
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
12

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
14_______1_______59
realm {
    syncEnabled = true
}
42
realm {
    syncEnabled = true
}
17

 

    

realm {
    syncEnabled = true
}
45

    

apply plugin: 'realm-android'     
65_______1_______96

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
98
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
52

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
76
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
78

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
80
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
08

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
10
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
12

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
14_______1_______59
realm {
    syncEnabled = true
}
68
realm {
    syncEnabled = true
}
17

 

    

realm {
    syncEnabled = true
}
71

    

apply plugin: 'realm-android'     
65_______1_______96

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
98
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
78

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
76
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
78

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
80
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
08

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
10
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
12

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
14
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
94
realm {
    syncEnabled = true
}
17

 

    

realm {
    syncEnabled = true
}
97

    

apply plugin: 'realm-android'     
65_______328_______00

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
98
apply plugin: 'realm-android'     
59plugins {04

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
76
apply plugin: 'realm-android'     
59
apply plugin: 'realm-android'     
78

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
80
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
08

realm {
    syncEnabled = true
}
2
realm {
    syncEnabled = true
}
10
apply plugin: 'realm-android'     
59
realm {
    syncEnabled = true
}
12

realm {
    syncEnabled = true
}
2plugins {18
apply plugin: 'realm-android'     
59plugins {20

realm {
    syncEnabled = true
}
2plugins {22
apply plugin: 'realm-android'     
59plugins {24
realm {
    syncEnabled = true
}
17

 

plugins {26_______1_______66

apply plugin: 'realm-android'     
91

Bước 6. Tạo một lớp phương thức để lưu trữ dữ liệu của chúng tôi

Điều hướng đến ứng dụng > java > tên gói ứng dụng của bạn > Nhấp chuột phải vào nó > Mới > Lớp Java và đặt tên là DataModal và thêm mã bên dưới vào đó.  

Java




'realm-android'2 plugins {30

'realm-android'2 plugins {32

 

'realm-android'8 'realm-android'9 plugins {35

apply plugin: 'realm-android'     
01 plugins {37

    plugins {39

    plugins {41

    plugins {43

    plugins {45 plugins {46 plugins {47

    plugins {45 plugins {50

    plugins {45 plugins {53

    plugins {45 plugins {56

    plugins {45 plugins {59

 

    

apply plugin: 'realm-android'     
14

    plugins {63

    'realm-android'8 plugins {66

    }

 

    plugins {70

    plugins {72

    'realm-android'8 plugins {75

realm {
    syncEnabled = true
}
2plugins {77 plugins {78

    }

 

    'realm-android'8

apply plugin: 'realm-android'     
07 plugins {84

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
19plugins {87

    }

 

    'realm-android'8 plugins {46 plugins {93

realm {
    syncEnabled = true
}
2plugins {77 plugins {47

    }

 

    334_______8

apply plugin: 'realm-android'     
07     02plugins {46     04

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
19    07

    }

 

    'realm-android'8     12

realm {
    syncEnabled = true
}
2plugins {77     15

    }

 

    'realm-android'8

apply plugin: 'realm-android'     
07     21

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
19    24

    }

 

    'realm-android'8     29

realm {
    syncEnabled = true
}
2plugins {77     32

    }

 

    'realm-android'8

apply plugin: 'realm-android'     
07     38

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
19    41

    }

 

    'realm-android'8     46

realm {
    syncEnabled = true
}
2plugins {77     49

    }

 

    'realm-android'8

apply plugin: 'realm-android'     
07     55

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
19    58

    }

}

Bước 7. Làm việc với MainActivity. tập tin java

Chuyển đến Hoạt động chính. java và tham khảo đoạn mã sau. Dưới đây là mã cho MainActivity. tập tin java. Nhận xét được thêm vào bên trong mã để hiểu mã chi tiết hơn

Java




'realm-android'2     63

'realm-android'2     65

'realm-android'2     67

'realm-android'2     69

'realm-android'2     71

'realm-android'2     73

 

'realm-android'2     75

 

'realm-android'2 'realm-android'5

 

'realm-android'8 'realm-android'9     80

apply plugin: 'realm-android'     
01     82

 

        84

    plugins {45     87

    plugins {45     90

    

        93

        95

    plugins {45     98

 

    

apply plugin: 'realm-android'     
04

    id02

apply plugin: 'realm-android'     
07 id04

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
10id07

realm {
    syncEnabled = true
}
2id09

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2id12

realm {
    syncEnabled = true
}
2id14

realm {
    syncEnabled = true
}
2id16

realm {
    syncEnabled = true
}
2id18

realm {
    syncEnabled = true
}
2id20

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2id23

realm {
    syncEnabled = true
}
2id25

realm {
    syncEnabled = true
}
2id27

realm {
    syncEnabled = true
}
2id29
apply plugin: 'realm-android'     
27 id31

    6

apply plugin: 'realm-android'     
04

    6'realm-android'8

apply plugin: 'realm-android'     
07 id37

apply plugin: 'realm-android'     
26

apply plugin: 'realm-android'     
26_______330_______40

apply plugin: 'realm-android'     
26_______330_______42

apply plugin: 'realm-android'     
26_______330_______44

apply plugin: 'realm-android'     
26_______330_______46

apply plugin: 'realm-android'     
26_______330_______48

apply plugin: 'realm-android'     
26

apply plugin: 'realm-android'     
26_______330_______51

apply plugin: 'realm-android'     
26_______330_______53 id54

id55_______330_______56____330_______57

apply plugin: 'realm-android'     
20

apply plugin: 'realm-android'     
26_______332_______id61 id53 id63

id55_______330_______65____330_______66

apply plugin: 'realm-android'     
20

apply plugin: 'realm-android'     
26_______332_______id61 id53 id72

id55_______330_______74____330_______75

apply plugin: 'realm-android'     
20

apply plugin: 'realm-android'     
26_______332_______id61 id53 id81

id55id83id84

apply plugin: 'realm-android'     
20

apply plugin: 'realm-android'     
26_______332_______id61 id89

id55id91

id55id93

id55id95_______1_______19id97id98id99

id55'com.android.application'01______331_______02

apply plugin: 'realm-android'     
20

id55'com.android.application'05'com.android.application'02

apply plugin: 'realm-android'     
20

id55'com.android.application'09'com.android.application'02

apply plugin: 'realm-android'     
20

id55'com.android.application'13____331_______02

apply plugin: 'realm-android'     
20

apply plugin: 'realm-android'     
26______332_______

    6}

realm {
    syncEnabled = true
}
2'com.android.application'21

    }

 

    plugins {45

apply plugin: 'realm-android'     
07 'com.android.application'27

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2'com.android.application'30

realm {
    syncEnabled = true
}
2'com.android.application'32

realm {
    syncEnabled = true
}
2'com.android.application'34
apply plugin: 'realm-android'     
27 'com.android.application'36

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2'com.android.application'39

realm {
    syncEnabled = true
}
2'com.android.application'41'realm-android'9'com.android.application'43'com.android.application'44
apply plugin: 'realm-android'     
20

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2
apply plugin: 'realm-android'     
14

realm {
    syncEnabled = true
}
2'com.android.application'50

realm {
    syncEnabled = true
}
2plugins {46 'com.android.application'53

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2'com.android.application'56

realm {
    syncEnabled = true
}
2id53 'com.android.application'59'com.android.application'60'com.android.application'61

    6'com.android.application'63

    6'com.android.application'65

    6'com.android.application'67______328_______3'com.android.application'69

realm {
    syncEnabled = true
}
2}id61 id89

    6'com.android.application'75

    6'com.android.application'77

    6'com.android.application'79plugins {3'com.android.application'69

realm {
    syncEnabled = true
}
2}

realm {
    syncEnabled = true
}
2'com.android.application'85

realm {
    syncEnabled = true
}
2'com.android.application'87

realm {
    syncEnabled = true
}
2'com.android.application'89

realm {
    syncEnabled = true
}
2'com.android.application'91

realm {
    syncEnabled = true
}
2'com.android.application'93

realm {
    syncEnabled = true
}
2'com.android.application'95

realm {
    syncEnabled = true
}
2'com.android.application'97

realm {
    syncEnabled = true
}
2

realm {
    syncEnabled = true
}
2}00

realm {
    syncEnabled = true
}
2}02
apply plugin: 'realm-android'     
27 }04

    6

apply plugin: 'realm-android'     
04

    6'realm-android'8

apply plugin: 'realm-android'     
07 }10

apply plugin: 'realm-android'     
26_______332_______12

apply plugin: 'realm-android'     
26_______332_______14

apply plugin: 'realm-android'     
26_______332_______16

    6}

realm {
    syncEnabled = true
}
2'com.android.application'21

    }

}

Bây giờ hãy chạy ứng dụng của bạn và xem đầu ra của ứng dụng. Trong bài viết này, bạn sẽ chỉ có thể thêm dữ liệu vào cơ sở dữ liệu của chúng tôi. Trong bài viết tiếp theo, chúng ta sẽ xem xét việc đọc dữ liệu này.