Android数据的四种存储方式之SQLite数据库
作者:网络转载 发布时间:[ 2014/6/23 13:31:54 ] 推荐标签:Android SQLite 数据库
// ===========sql语句方式操作SQLite数据库================================
|
public void insert() {
db.execSQL("insert into person(name, phone, money) values(?, ?, ?)",
new Object[] { "大明", "18666", 6000 });
db.execSQL("insert into person(name, phone, money) values(?, ?, ?)",
new Object[] { "小红", "18666", 6000 });
db.execSQL("insert into person(name, phone, money) values(?, ?, ?)",
new Object[] { "大红", "18666", 6000 });
}
public void delete() {
db.execSQL("delete from person where name = ?", new Object[] { "大明" });
}
public void update() {
db.execSQL("update person set money = 10000 where name = ?",
new Object[] { "小明" });
}
public void query() {
// 游标,存放查询返回的数据,获取方法跟resultSet高度雷同
Cursor c = db.rawQuery("select * from person", new String[] {});
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndex("name"));
String phone = c.getString(c.getColumnIndex("phone"));
System.out.println(name + ";" + phone);
}
}
|
// ==============谷歌提供的api对SQLite数据库的操作======================
|
/**
* api-insert data to table
*/
public void insertApi() {
ContentValues cv = new ContentValues();
cv.put("name", "微明");
cv.put("phone", 15666);
cv.put("money", 630);
long id = db.insert("person", null, cv);
System.out.println(id);
}
/**
* api-delete data from table
*
* @return the number of rows affected
*/
public int deleteApi() {
int affectedNum = db.delete("person", "name=?", new String[] { "小红" });
return affectedNum;
}
/**
* api-update
*/
public void updateApi() {
ContentValues contentValues = new ContentValues();
contentValues.put("name", "小红");
contentValues.put("money", "10");
// return the number of rows affected
db.update("person", contentValues, "name=?", new String[] { "大红" });
}
public void queryApi() {
Cursor cursor = db.query("person", new String[] { "phone", "money" },
null, null, null, null, null);
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String money = cursor.getString(cursor.getColumnIndex("money"));
System.out.println(phone + "##" + money);
}
}
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
Android自动化测试框架有哪些?有什么用途?Android测试中最容易忽略的测试点有哪些?Android 手机自动化测试工具有哪几种?移动APP测试之android性能测试快速提升Android App 的代码覆盖率Android Unit Test 框架比较Android单元测试框架Robolectric3.0介绍(一)Android单元测试的整理在Android Studio中实现单元测试Android连接MySQL方法,测试成功关于Android MVP模式的思考Android 数据库管理?ActiveAndroid编写Android测试单元该做的和不该做的事Android阿里面试Java基础锦集在Android项目中使用Java8Java / Android 面试中所遇到的那些坑

sales@spasvo.com