当前位置:   article > 正文

鸿蒙HarmonyOS关系型数据库创建及增删改查代码_鸿蒙os连接sql

鸿蒙os连接sql

public class MainAbilitySlice extends AbilitySlice {
//创建数据库需要的回调
    private static 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, salary REAL)");
        }
        @Override
        public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
        }
    };
    //开启数据库需要的回调 虽然这回调啥都不用写 但是需要有
    private static final RdbOpenCallback opencallback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button cdb = (Button) findComponentById(ResourceTable.Id_create_db_button);
        Button odb = (Button) findComponentById(ResourceTable.Id_open_db_button);

        cdb.setClickedListener(c->{
            //创建数据库
            StoreConfig config = StoreConfig.newDefaultConfig("RdbStoreTest.db");
            DatabaseHelper helper = new DatabaseHelper(MainAbilitySlice.this);
            RdbStore rdbStore = helper.getRdbStore(config, 1, callback, null);

            //通过谓词的方式插入数据到test表
            ValuesBucket valuesBucket = new ValuesBucket();
            valuesBucket.putInteger("id", 1);
            valuesBucket.putString("name", "name1");
            valuesBucket.putInteger("age", 28);
            valuesBucket.putDouble("salary", 8000.5);
            long rowid1 = rdbStore.insert("test", valuesBucket);

            valuesBucket.putInteger("id", 2);
            valuesBucket.putString("name", "name2");
            valuesBucket.putInteger("age", 28);
            valuesBucket.putDouble("salary", 10000.5);

            long rowid2 = rdbStore.insert("test", valuesBucket);
            valuesBucket.putInteger("id", 3);
            valuesBucket.putString("name", "name3");
            valuesBucket.putInteger("age", 28);
            valuesBucket.putDouble("salary", 10000.5);

            long rowid3= rdbStore.insert("test", valuesBucket);
            valuesBucket.putInteger("id", 4);
            valuesBucket.putString("name", "name4");
            valuesBucket.putInteger("age", 28);
            valuesBucket.putDouble("salary", 10000.5);

            long rowid4= rdbStore.insert("test", valuesBucket);
             //用原生的sql来做插入 没有注释的代码可用,注释的代码不可用
            rdbStore.executeSql("insert into test (id,name,age,salary) values (5,'lisi',28,5000.01)");
            rdbStore.executeSql("insert into test (id,name,age,salary) values (?,'name8',28,5000.01)"
                    ,new Object[]{8});
//            rdbStore.executeSql("insert into test (id,name,age,salary) values (?,'?',28,5000.01)"
//                    ,new String[]{"6","name6"});
//            rdbStore.executeSql("insert into test (id,name,age,salary) values (?,'?',28,5000.01)"
//                    ,new Object[]{7,"name7"});

//关闭数据库
            rdbStore.close();
        });

        odb.setClickedListener(c->{
        //查询数据库之前 先得到数据库 注意回调opencallback和创建的时候不一样
            StoreConfig config = StoreConfig.newDefaultConfig("RdbStoreTest.db");
            DatabaseHelper helper = new DatabaseHelper(MainAbilitySlice.this);
            RdbStore rdbStore = helper.getRdbStore(config, 1, opencallback, null);
            ValuesBucket replaceVB = new ValuesBucket();
            //更新数据
            replaceVB.putString("name","change1");

            RdbPredicates replace_rdbPredicates = new RdbPredicates("test").equalTo("id", 1);

            int update = rdbStore.update(replaceVB, replace_rdbPredicates);
            //用原生的sql来做更新
            rdbStore.executeSql("update test set name='change2' where id=?",new Object[]{2});

//删除数据
            //通过谓词的方式删
        rdbStore.delete(new RdbPredicates("test").equalTo("id",3));
            //原生sql:
            rdbStore.executeSql("delete from test where id=?",new Object[]{4});


//查询数据库
            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")));
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/294880
推荐阅读
相关标签
  

闽ICP备14008679号