1.1. Components
This component simplifies the connection to the SQLite database based on the HarmonyOS database. It also encapsulates the HarmonyOS native API, making it easier to read and write sqLite databases. 1.2. Running effect on mobile phone simulator
Succeeded in inserting data
2. Use method of ActiveOhos
2.1. Add sqlitelibrary-debug.har package dependencies for the application
When calling HAR in an application module, the usual way to add a dependency is to depend on a local HAR
Step 1: Copy sqlitelibrary-debug. Har to entry\libs (no further changes are needed because the build.gradle already relies on *. Har from libs).
Check if *. Har exists under build.gradle in the project directory
Step 2: In addition to the dependency HAR, we also need to add external dependencies to implement the introduction of the class. The introduction method is as follows. After the introduction, synchronization can be used.
-
If the module that uses the annotation handler is “com.huawei.ohos.hap”, you need to add the following configuration to the “ohos” node of the module “build.gradle” file:
compileOptions{ annotationEnabled true }
-
If the module that uses the annotation handler is com.huawei.ohos.library, configure the annotation handler in the Dependencies node of the build.gradle file of the module. Jar, ORM_annotationS_processor_java. jar, and Javapoet_java. jar directories in the HUAWEI SDK. And import the three JAR packages into the project.
Dependencies {compile files(“orm_annotations_java.jar”
, orm_annotationS_processor_java.jar,javapoet_java.jar)
AnnotationProcessor Files (“orm_annotations_java.jar”
, orm_annotationS_processor_java.jar,javapoet_java.jar)}
-
If the module that uses the annotation handler is “java-library”, configure the annotation handler in the “Dependencies” node of the module “build.gradle” file and import “ohos.jar”.
-
Dependencies {compile files(“ohos.jar “,”orm_annotations_java.jar”
-
“,” orm_annotationS_processor_java.jar path “,” Javapoet_java.jar path “)
-
AnnotationProcessor Files (“orm_annotations_java.jar”
-
“,” orm_annotationS_processor_java.jar path “,” Javapoet_java.jar path “)}
-
Such as:
-
After the above operation is correct, you can code!
-
3. ActiveOhos development and implementation
-
3.1. Main page layout file
-
Define four buttons to achieve increase, delete, change and check respectively, define four buttons to achieve request click events
ohos:width=”match_content” ohos:background_element=”$graphic:background_ability_main” Ohos :layout_alignment=”horizontal_center” oHOs :text=”get request “OHOs :text_size=”50″ oHOs :top_margin=”80vp” />
-
3.2. The example code is as follows
The OrmContext connection method is used to define an entity class (User) that corresponds to the table name and fields of the database, and a BookStore class that corresponds to the database. The code is as follows:
MainAbilitySliceimport com.example.myapplication.BookStore; import com.example.myapplication.ResourceTable; import com.example.myapplication.User; import com.example.sqlitelibrary.DBManage; import com.example.sqlitelibrary.DBOrmContext; import com.example.sqlitelibrary.utils.Log; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.data.DatabaseHelper; import ohos.data.orm.OrmContext; import ohos.data.orm.OrmPredicates; import ohos.data.rdb.RdbStore; import ohos.data.rdb.ValuesBucket; import java.util.ArrayList; import java.util.List; public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { private DatabaseHelper helper; private RdbStore store; private OrmContext context; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); helper = new DatabaseHelper(this); DBManage dbManger = new DBManage(“user.db”,”user”); context = dbManger.getConnectionContext(helper, BookStore.class); // DBManage dbManger = new DBManage(“user.db”); // store = dbManger.getConnectionStore(helper,”user”); Button btnInsert = (Button) findComponentById(ResourceTable.Id_btn_insert); Button btnQuery = (Button) findComponentById(ResourceTable.Id_btn_query); Button btnDelete = (Button) findComponentById(ResourceTable.Id_btn_delete); Button btnUpdate = (Button) findComponentById(ResourceTable.Id_btn_update); btnInsert.setClickedListener(this::onClick); btnQuery.setClickedListener(this::onClick); btnDelete.setClickedListener(this::onClick); btnUpdate.setClickedListener(this::onClick); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override public void onClick(Component component) {// RdbStoreManage rdbStoreMange = new RdbStoreManage(); // ValuesBucket values = new ValuesBucket(); // values.putInteger(“id”, 1); // values.putString(“name”, “zhangsan”); // values.putInteger(“age”, 18); / / values. PutDouble (” salary “, 100.5); // values.putByteArray(“blobType”, new byte[] {1, 2, 3}); // rdbstoremange. setSql(store, “insert into user values(zhangsan, 18, 100.5, byte[1,2,3])”); // long id = rdbStoreMange.insert(store,”user”, values); // System.out.println(id); DBOrmContext dbOrmContext = new DBOrmContext(); switch (component.getId()) { case ResourceTable.Id_btn_insert: User user = new user (); user.setFirstName(“Zhang”); user.setLastName(“San”); user.setAge(29); User. SetBalance (100.51); boolean b = dbOrmContext.insert(context, user); Log. I (” insert successfully “); System.out.println(b); break; Case resourcetable. Id_btn_query: // Conditional query List users = new ArrayList<>(); OrmPredicates query = context.where(User.class).equalTo(“lastName”, “San”); users = dbOrmContext.query(context, query); break; Case resourcetable. Id_btn_delete: OrmPredicates DELETE = context.where(user.class).equalto (“lastName”, “San”); int delete1 = dbOrmContext.delete(context, delete); System.out.println(delete1); break; Case resourcetable. Id_btn_update: // Conditional update ValuesBucket ValuesBucket = new ValuesBucket(); valuesBucket.putInteger(“age”, 31); valuesBucket.putString(“firstName”, “Zhang”); valuesBucket.putString(“lastName”, “San”); ValuesBucket. PutDouble (” balance “, 300.51); OrmPredicates update = context.where(User.class).equalTo(“userId”, 1); int update1 = dbOrmContext.update(context, valuesBucket, update); System.out.println(update1); break; } dbOrmContext.flush(context); }}user.java@Entity(tableName = “user”, ignoredColumns = {“ignoreColumn1”, “ignoreColumn2”}, indices = {@Index(value = {“firstName”, “lastName”}, name = “name_index”, Unique = true)})public class User extends OrmObject {// Here the userId is set to an incrementable primary key. Note that the auto-increment primary key works only if the data type is wrapper. @PrimaryKey(autoGenerate = true) private Integer userId; private String firstName; private String lastName; private int age; private double balance; private int ignoreColumn1; private int ignoreColumn2; // Add your own getter and setter methods for fields
-
Project source code address: github.com/isoftstone-…
-
Welcome to exchange: [email protected]
The original link: developer.huawei.com/consumer/cn…
Original author: ISofpass HOS