赞
踩
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); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。