Android数据的四种存储方式之SQLite数据库

发表于:2014-6-23 09:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:blfshiye    来源:51Testing软件测试网采编

  // ===========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);
}
}
32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号