80,447
社区成员
发帖
与我相关
我的任务
分享
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
SQLiteDatabase db=mydb.getWritableDatabase();
long newid;
Uri uri2=null;
switch (uriMatcher.match(uri)) {
case PEOPLE_TABLE:
case PEOPLE_ITEM:
newid=db.insert("people", null, values);
uri2=Uri.parse(AUTHORITY+"/people/"+newid);
break;
case PEOPLEID_TABLE:
case PEOPLEID_ITEM:
newid=db.insert("peopleid", null, values);
uri2=Uri.parse(AUTHORITY+"/peopleid/"+newid);
break;
default:
break;
}
return uri2;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
SQLiteDatabase db=mydb.getWritableDatabase();
int deleteRows=0;
String deleteId=uri.getPathSegments().get(1);//获取传入的uri最后的id,使用的是uri.getPathSegments().get(1)
switch (uriMatcher.match(uri)) {
case PEOPLE_TABLE:
deleteRows=db.delete("people", selection, selectionArgs);
break;
case PEOPLE_ITEM:
deleteRows=db.delete("people", "id=?", new String[]{deleteId});
break;
case PEOPLEID_TABLE:
deleteRows=db.delete("peopleid", selection, selectionArgs);
break;
case PEOPLEID_ITEM:
deleteRows=db.delete("peopleid", "id=?", new String[]{deleteId});
break;
default:
break;
}
return deleteRows;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_add:
uri=Uri.parse("content://com.example.sqlite.myprovider/people");
ContentValues values=new ContentValues();
values.put("name", "张三"+i);
values.put("age", 20+i);
values.put("sex", "男");
values.put("money", 23.12+i);
Uri newUri=cs.insert(uri, values);
newID=newUri.getPathSegments().get(1);//然后改为get(2)就一切正常了
textView.setText(newUri.toString()+"\n"+newUri.getPathSegments().toString()+"\n"+newID);
i++;
values.clear();
break;
case R.id.btn_query:
uri=Uri.parse("content://com.example.sqlite.myprovider/people");
Cursor cursor=cs.query(uri, null, null, null, null);
show(cursor, textView, sb);
break;
case R.id.btn_delete:
uri=Uri.parse("content://com.example.sqlite.myprovider/people/"+newID);
cs.delete(uri, null, null);
break;
case R.id.btn_update:
uri=Uri.parse("content://com.example.sqlite.myprovider/people/"+newID);
ContentValues values1=new ContentValues();
values1.put("name", "李四");
cs.update(uri, values1, null, null);
break;
default:
break;
}
}