增刪改查(按照上面的例子)
String sql=”insert into student values(null,”張三”,18,“三”)”;db.execSQL(sql)//添加
String sql =“delete from student where _id=2”;db.execSQL(sql)//刪除
String sql =”update student set nick =‘四’where _id=1;db.execSQL(sql)//改
//查
Cursor cursor=db.rawQuery(“select* from student”,null);
cursor.moveToNext();//移動到下一行
int name =cursor.getColumnIndex(“name”)//獲取列的索引
String nameStr=cursor.getString(name);
google api的增刪改查
//添加
//參1表格
//參2 如果插入數據少字段,允許容錯
//參3map集合
ContentValues values = new ContentValues(); values .add(“name”,”李四”);
long id =db.insert(“student”,”“,values );
if(id!=-1){System.out.println(“添加成功”)}
//刪除
//參1 表名
//參2條件
//參3字符串數據,替換條件裏的佔位符
int id =db.delete(“student”,”_id=?”, new String[]{“2”});
if(id>0){System.out.println(“刪除成功”)};
//修改
//參1表名
//參2ContentValues 用來對應表中的字段和值
//參3條件
//參4是字符串數據,替換條件裏的佔位符
ContentValues values = new ContentValues();values .put(“nick”,”五”);
int id=db.update(“student”,values,”_id=?”,new String[]{“2”});
if(id>0){System.out.println(“更新成功”)};
//查詢
//參1 表名
//參2 查詢的列
//參3查詢的條件
//參4 字符串數據,替換條件裏的佔位符
//參5 分組查詢
//參6having的條件
//參7 排序
//參8分頁
Cursor cursor =db.query(“student”,null,null,null,null,null,null,null);
while(cursor .moveToNext()){
int name =cursor.getColumnIndex(“name”)//獲取列的索引
String nameStr=cursor.getString(name);
}