Android SQLite的使用2(非原创)
1.数据库的增、删、改、查:execSQL方法
public void insertAction() {//添加信息
db.execSQL("insert into Emp(name,salary) values('张无忌','12000')");
db.execSQL("insert into Emp(name,salary) values('赵敏','11000')");
db.execSQL("insert into Emp(name,salary) values('谢逊','16000')");
}
public void deleteAction() {//删除信息
db.execSQL("delete from Emp where name = '赵敏'");
}
public void updateAction() {//修改信息
db.execSQL("update Emp set salary = '18000' where name = ?",new Object[] { "谢逊" });
}
public void selectAction() {//查询信息
Cursor c = db.rawQuery("select * from Emp", null);//Cursor是游标,相当于ResultSet
while (c.moveToNext()) {//移动到下一个位置
String id = c.getString(c.getColumnIndex("id"));//获取“id”属性的索引
String name = c.getString(c.getColumnIndex("name"));
String salary = c.getString(c.getColumnIndex("salary"));
System.out.println(id + " , " + name + " , " + salary);
}
}