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:crate_db_button" ohos:height="match_content" ohos:width="match_content" ohos:text="创建数据库" ohos:text_size="20vp" ohos:text_color="#ffff" ohos:background_element="$graphic:button_bg" ohos:top_padding="8vp" ohos:left_padding="50vp" ohos:right_padding="50vp" ohos:layout_alignment="horizontal_center" ohos:top_margin="30vp" ohos:text_weight="700"/> <Button ohos:id="$+id:open_db_button" ohos:height="match_content" ohos:width="match_content" ohos:text_size="20vp" ohos:text="打开数据库" ohos:text_color="#ffff" ohos:background_element="$graphic:button_bg" ohos:top_padding="8vp" ohos:left_padding="50vp" ohos:right_padding="50vp" ohos:layout_alignment="horizontal_center" ohos:top_margin="30vp" ohos:text_weight="700"/> </DirectionalLayout>
按钮样式
<?xml version="1.0" encoding="UTF-8" ?> <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:shape="rectangle"> <corners ohos:radius="15"/> <solid ohos:color="blue"/> </shape>
2.
创建和启动数据库的代码分开来写,先初始化按钮
然后将前面文章所写创建和打开的代码分别放入到两个按钮当中,
创建数据库
cdb = (Button) findComponentById(ResourceTable.Id_crate_db_button); cdb.setClickedListener(c->{ // 创建数据库 StoreConfig.Builder builder = new StoreConfig.Builder(); String keystr = "123456"; builder.setName("stan.db") .setSyncMode(null) .setJournalMode(StoreConfig.JournalMode.MODE_WAL) .setStorageMode(StoreConfig.StorageMode.MODE_DISK) .setReadOnly(false) .setDatabaseFileType(DatabaseFileType.NORMAL) //加密 .setEncryptKey(keystr.getBytes()); StoreConfig config = builder.build(); //创建数据库的实质循环,重要的核心类 DatabaseHelper helper=new DatabaseHelper(MainAbilitySlice.this); //进一步通过helper,获取数据库对象 //传入四个参数 RdbStore rdbStore = helper.getRdbStore(config, 1, Callback, null); ValuesBucket valuesBucket = new ValuesBucket(); valuesBucket.putInteger("id",1); valuesBucket.putString("name","zhangsan"); valuesBucket.putInteger("age", 28); valuesBucket.putDouble("salary", 8000.5); long test = rdbStore.insert("test", valuesBucket); System.out.println("========test"+test); valuesBucket.putInteger("id",2); valuesBucket.putString("name","wangwu"); valuesBucket.putInteger("age", 28); valuesBucket.putDouble("salary", 80001.5); long test1 = rdbStore.insert("test", valuesBucket); System.out.println("========test"+test1); });
读取数据
odb = (Button) findComponentById(ResourceTable.Id_open_db_button); odb.setClickedListener(component -> { // 打开已经存在的数据库 StoreConfig.Builder builder = new StoreConfig.Builder(); String keystr = "123456"; builder.setName("stan.db") .setSyncMode(null) .setJournalMode(StoreConfig.JournalMode.MODE_WAL) .setStorageMode(StoreConfig.StorageMode.MODE_DISK) .setReadOnly(false) .setDatabaseFileType(DatabaseFileType.NORMAL) //加密 .setEncryptKey(keystr.getBytes()); StoreConfig config = builder.build(); DatabaseHelper helper=new DatabaseHelper(MainAbilitySlice.this); //进一步通过helper,获取数据库对象 //传入四个参数 RdbStore rdbStore = helper.getRdbStore(config, 1, openCallback, null); ResultSet resultSet = rdbStore. querySql("select id,name,age,salary from test where age=? order by salary desc", new String[]{"28"}); for (int i=0;i<resultSet.getRowCount();i++){ resultSet.goToRow(i); System.out.println("+++++++++id"+resultSet.getInt(resultSet.getColumnIndexForName("id"))); System.out.println("+++++++++name"+resultSet.getString(resultSet.getColumnIndexForName("name"))); System.out.println("+++++++++age"+resultSet.getInt(resultSet.getColumnIndexForName("age"))); System.out.println("+++++++++salary"+resultSet.getDouble(resultSet.getColumnIndexForName("salary"))); } });
3.演示效果
创建后打开数据库
网络异常,图片无法展示
|