我想在数据库表用户中使用我的自定义提供程序userProvider插入用户记录。但getcontentresolver.insert始终返回null。我不知道什么是错误....
我的自定义提供程序类:
public class userProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.codeworm.pickmeup";
static final String URL = "content://" + PROVIDER_NAME + "/user";
static final Uri CONTENT_URI = Uri.parse(URL);
private SQLiteDatabase db;
static final String DATABASE_NAME = "pickmeupDB";
static final String USER_TABLE_NAME = "user";
static final int DATABASE_VERSION = 2;
static final String CREATE_DB_TABLE =
" CREATE TABLE " + USER_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT NOT NULL, " +
" email TEXT NOT NULL,pass TEXT NOT NULL,phone TEXT NOT NULL," +
"ride TEXT NOT NULL,city TEXT NOT NULL,country TEXT NOT NULL,location TEXT NOT NULL);";
static final String _ID = "_id";
static final String NAME = "name";
static final String EMAIL = "email";
static final String PASS = "pass";
static final String PHONE = "phone";
static final String RIDE = "ride";
static final String CITY = "city";
static final String COUNTRY = "country";
static final String LOCATION = "location"+CITY+COUNTRY;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
onCreate(db);
}
}
static final int user = 1;
static final int getride = 2;
static final int shareride = 3;
static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "user", user);
//uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID);
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
return (db == null)? false:true;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert( USER_TABLE_NAME, "", values);
/**
* If record is added successfully
*/
if (rowID > 0)
{
// this response that data is inserted and what is the URI of the inserted data
Uri _uri=Uri.withAppendedPath(CONTENT_URI, "" + rowID);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor c;
if (sortOrder == null || sortOrder == ""){
// By default sort on student names
sortOrder = _ID;
}
switch (uriMatcher.match(uri)) {
case user:
c=db.query(USER_TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
break;
/*case STUDENT_ID:
c=db.query(STUDENTS_TABLE_NAME,projection,
_ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? " AND (" +selection + ')' : "")
,selectionArgs,null,null,sortOrder);
break;*/
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case user:
count = db.delete(USER_TABLE_NAME, selection, selectionArgs);
break;
/*case STUDENT_ID:
String id = uri.getPathSegments().get(1);
count = db.delete( STUDENTS_TABLE_NAME, _ID + " = " + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;*/
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case user:
count = db.update(USER_TABLE_NAME, values, selection, selectionArgs);
break;
/* case STUDENT_ID:
count = db.update(STUDENTS_TABLE_NAME, values, _ID + " = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" +selection + ')' : ""), selectionArgs);
break;*/
default:
throw new IllegalArgumentException("Unknown URI " + uri );
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Nullable
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri))
{
/**
* Get all student records
*/
case user:
return "vnd.android.cursor.dir/vnd.example.user";
/**
* Get a particular student
*/
/*case STUDENT_ID:
return "vnd.android.cursor.item/vnd.example.students";*/
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
主要课程:
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(userProvider.NAME, (name.getText().toString()));
values.put(userProvider.EMAIL, (email.getText().toString()));
values.put(userProvider.PASS, (pass.getText().toString()));
values.put(userProvider.PHONE, (phone.getText().toString()));
values.put(userProvider.CITY, (city.getText().toString()));
values.put(userProvider.COUNTRY, (country.getText().toString()));
values.put(userProvider.LOCATION, (locatioin.getText().toString()));
if (ride) {
values.put(userProvider.RIDE, "True".toString());
} else {
values.put(userProvider.RIDE, "False".toString());
}
uri = getContentResolver().insert(userProvider.CONTENT_URI, values);
}
});