#2020征文-手机#HarmonyOS关系型数据库初体验
遇到问题
1 RawRdbPredidates类不存在
2 事务不生效
<?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">
<Text
ohos:id="$+id:text_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="Hello World"
ohos:text_size="50"
/>
<Button
ohos:height="match_content"
ohos:width="match_content"
ohos:text="添加"
ohos:text_size="100px"
ohos:id="$+id:add"></Button>
<Button
ohos:height="match_content"
ohos:width="match_content"
ohos:text="查询"
ohos:text_size="100px"
ohos:id="$+id:get"></Button>
<Button
ohos:height="match_content"
ohos:width="match_content"
ohos:text="删除"
ohos:text_size="100px"
ohos:id="$+id:del"></Button>
<Button
ohos:height="match_content"
ohos:width="match_content"
ohos:text="更改"
ohos:text_size="100px"
ohos:id="$+id:upd"></Button>
<Button
ohos:height="match_content"
ohos:width="match_content"
ohos:text="模拟事务"
ohos:text_size="100px"
ohos:id="$+id:tran"></Button>
</DirectionalLayout>
package com.datang.myapplication.slice;
import com.datang.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
public class MainAbilitySlice extends AbilitySlice {
HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
RdbStore store = null;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initDB();
insert();
select();
delete();
update();
tran();
}
@Override
public void onActive() {
super.onActive();
}
public void initDB() {
//数据库名称
StoreConfig config = StoreConfig.newDefaultConfig("RdbStoreTest.db");
//初始化
RdbOpenCallback callback = new RdbOpenCallback() {
@Override
public void onCreate(RdbStore store) {
//创建表
store.executeSql("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER)");
//创建表2
store.executeSql("CREATE TABLE IF NOT EXISTS test2 (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER)");
//删除表2
store.executeSql("DROP TABLE test2");
HiLog.info(label, ">>>>>>>>>>>>>>>>>>>>>>>RdbOpenCallback.onCreate");
}
@Override
public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
HiLog.info(label, ">>>>>>>>>>>>>>>>>>>>>>>RdbOpenCallback.onUpgrade");
}
};
DatabaseHelper helper = new DatabaseHelper(this);
//创建
store = helper.getRdbStore(config, 1, callback, null);
}
//增加
public void insert() {
Button button = (Button) findComponentById(ResourceTable.Id_add);
button.setClickedListener(e -> {
ValuesBucket values = new ValuesBucket();
for (int i = 0; i < 10; i++) {
values.putString("name", "zhangsan" + (i + 1));
values.putInteger("age", i + 1);
long id = store.insert("test", values);
HiLog.info(label, "添加后的id%{public}d", id);
}
});
}
//查询
public void select() {
Button button2 = (Button) findComponentById(ResourceTable.Id_get);
button2.setClickedListener(e -> {
String[] columns = new String[]{"id", "name", "age"};
RdbPredicates rdbPredicates = new RdbPredicates("test");
//类似于这样的条件蛮多的。不加就是没有过滤条件
//rdbPredicates.equalTo("","");
//rdbPredicates.equalTo()
ResultSet resultSet = store.query(rdbPredicates, columns);
//获取结果集总行
int count = resultSet.getRowCount();
HiLog.info(label, "查询是否成功%{public}s", count);
//控制结果集指针,类似于迭代器一样。
while (resultSet.goToNextRow()) {
int id = resultSet.getInt(0);
String name = resultSet.getString(1);
int age = resultSet.getInt(2);
HiLog.info(label, "结果集》》》id:%{public}d,name:%{public}s,age:%{public}d:", id, name, age);
}
//并没有该类,不知道是不是API未开放
//RawRdbPredidates:仅支持设置表名、where条件子句、whereArgs三个参数,不支持equalTo等接口调用。
HiLog.info(label, "--------------------------------------------");
//这是另一种查询方式,直接输入SQL
ResultSet resultSet1 = store.querySql("select * from test", null);
int count2 = resultSet.getRowCount();
HiLog.info(label, "查询是否成功%{public}s", count2);
while (resultSet1.goToNextRow()) {
int id = resultSet1.getInt(0);
String name = resultSet1.getString(1);
int age = resultSet1.getInt(2);
HiLog.info(label, "结果集》》》id:%{public}d,name:%{public}s,age:%{public}d:", id, name, age);
}
});
}
//删除
public void delete() {
Button button3 = (Button) findComponentById(ResourceTable.Id_del);
button3.setClickedListener(e -> {
RdbPredicates rdbPredicates = new RdbPredicates("test");
rdbPredicates.equalTo("id", 1);
int id = store.delete(rdbPredicates);
HiLog.info(label, "删除的id%{public}d", id);
//并没有该类,不知道是不是API未开放
//RawRdbPredidates:仅支持设置表名、where条件子句、whereArgs三个参数,不支持equalTo等接口调用。
});
}
//更改
public void update() {
Button button4 = (Button) findComponentById(ResourceTable.Id_upd);
button4.setClickedListener(e -> {
ValuesBucket values = new ValuesBucket();
values.putString("name", "李四");
RdbPredicates rdbPredicates = new RdbPredicates("test");
rdbPredicates.equalTo("id", 1);
int id = store.update(values, rdbPredicates);
HiLog.info(label, "更改的id%{public}d", id);
//并没有该类,不知道是不是API未开放
//RawRdbPredidates:仅支持设置表名、where条件子句、whereArgs三个参数,不支持equalTo等接口调用。
});
}
//模拟事务
public void tran() {
Button button5 = (Button) findComponentById(ResourceTable.Id_tran);
button5.setClickedListener(e -> {
try {
store.beginTransaction();
ValuesBucket values = new ValuesBucket();
for (int i = 0; i < 10; i++) {
values.putString("name", "zhangsan" + (i + 1));
values.putInteger("age", i + 1);
long id = store.insert("test", values);
HiLog.info(label, "添加后的id%{public}d", id);
}
//按我的理解,这里异常后应该会回滚的,但是实际上并没有!
int i = 1 / 0;
store.markAsCommit();
store.endTransaction();
} catch (Exception s) {
HiLog.info(label, "异常了----------------------" + e);
}
});
}
}
另外请教个问题。创建好的sqllite数据库在哪个文件夹下?如何查看?