插入數據成功java
ActiveOhos使用方法git
2.1. 爲應用添加sqlitelibrary-debug.har包依賴github
在應用模塊中調用HAR,經常使用的添加依賴爲:依賴本地HARsql
第一步:將sqlitelibrary-debug.har複製到entry\libs目錄下便可(因爲build.gradle中已經依賴的libs目錄下的*.har,所以不須要再作修改)。數據庫
查看工程目錄中build.gradle下的*.har是否存在app
第二步:除了依賴har以外還須要添加外部依賴用來實現類的引入,引入方式以下,引入完以後同步便可使用。ide
若是使用註解處理器的模塊爲「com.huawei.ohos.hap」,則須要在模塊 「build.gradle」文件的「ohos」節點中添加如下配置:佈局
compileOptions{ annotationEnabled true }
若是使用註解處理器的模塊爲「com.huawei.ohos.library」,則須要在模塊「build.gradle」文件的「dependencies」節點中配置註解處理器。查看「orm_annotations_java.jar」、「orm_annotations_processor_java.jar」 、「javapoet_java.jar」 3個jar包在HUAWEI SDK中的對應目錄,並將這三個jar包導入項目中。gradle
dependencies { compile files("orm_annotations_java.jar的路徑ui
,orm_annotations_processor_java.jar的路徑,javapoet_java.jar的路徑)
annotationProcessor files("orm_annotations_java.jar的路徑
,orm_annotations_processor_java.jar的路徑,javapoet_java.jar的路徑)}
若是使用註解處理器的模塊爲「java-library」,則須要在模塊 「build.gradle」文件的「dependencies」節點中配置註解處理器,並導入「ohos.jar」。
dependencies { compile files("ohos.jar的路徑","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的路徑")}
好比:
以上操做無誤 以後就能夠進行編碼了!
3.1. 主頁面的佈局文件
定義四個按鈕分別實現增刪改查,定義四個Button實現請求點擊事件
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:orientation="vertical"> <Button ohos:id="$+id:btn_insert" ohos:height="match_content" ohos:width="80fp" ohos:text_color="red" ohos:text="插入" ohos:text_size="20fp" ohos:weight="100fp"/> <Button ohos:id="$+id:btn_query" ohos:height="match_content" ohos:width="100fp" ohos:text_color="blue" ohos:text="查詢" ohos:text_size="20fp" ohos:weight="100fp"/> <Button ohos:id="$+id:btn_update" ohos:height="match_content" ohos:width="100fp" ohos:text_color="green" ohos:text="更新" ohos:text_size="20fp" ohos:weight="100fp"/> <Button ohos:id="$+id:btn_delete" ohos:height="match_content" ohos:width="100fp" ohos:text_color="black" ohos:text="刪除" ohos:text_size="20fp" ohos:weight="100fp"/> <ListContainer ohos:id="$+id:listText" ohos:height="match_parent" ohos:width="match_parent"/> </DirectionalLayout> ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="get請求" ohos:text_size="50" ohos:top_margin="80vp" /> </DirectionalLayout>
3.2. 例子代碼以下
組件中有兩種鏈接數據的方式,分別是OrmContext,RdbStore ,其中使用OrmContext鏈接方式時,須要定義一個實體類(User)來和數據庫對應表名及字段,一個數據庫類 BookStore 來配合開發,代碼以下:
MainAbilitySlice import 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 user = new User(); user.setFirstName("Zhang"); user.setLastName("San"); user.setAge(29); user.setBalance(100.51); boolean b = dbOrmContext.insert(context, user); Log.i("插入成功"); System.out.println(b); break; case ResourceTable.Id_btn_query: //條件查詢 List<User> 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: //條件更新 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 { // 此處將userId設爲了自增的主鍵。注意只有在數據類型爲包裝類型時,自增主鍵才能生效。 @PrimaryKey(autoGenerate = true) private Integer userId; private String firstName; private String lastName; private int age; private double balance; private int ignoreColumn1; private int ignoreColumn2;
// 開發者自行添加字段的getter和setter 方法
項目源代碼地址:https://github.com/isoftstone...
歡迎交流:HWIS-HOS@isoftstone.com
原文連接:https://developer.huawei.com/...原做者:軟通動力HOS