您好我正在创建自己的内容提供商,这是创建代码的数据库:
public static final Uri CONTENT_URI =
Uri.parse("content://"+ PROVIDER_NAME + "/user");
public static final String _ID = "userid";
public static final String Name = "name";
public static final String Email_id= "email";
public static final String Login_id = "login_id";
public static final String Password = "password";
public static final String Phonenumber = "phonenumber";
public static final String Address = "address";
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE +
" (userid integer primary key autoincrement,name text not null"+
" email text not null,login_id text not null,password text not null"+
"phonenumber text not null,address text not null);";
插入方法如下
public Uri insert(Uri uri, ContentValues values) {
long rowID = userDB.insert(
DATABASE_TABLE, null, values);
if (rowID>0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
并使用下面的代码
来设置值 values.put("name",ame);
values.put("email",Email);
values.put("login_id",Login);
values.put("phonenumber",val1);
values.put("address",Address);
Uri uri=getContentResolver().insert(Uri.parse("content://"+PROVIDER_NAME+"/user"),values);
throw new SQLException("Failed to insert row into " + uri);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我收到以下错误。
Error inserting name=qwe
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:150)
at com.example.contentprovider.implement.insert(implement.java:128)
Uncaught remote exception! (Exceptions are not yet supported across processes.)
android.database.SQLException: Failed to insert row into content://net.learn2develop.provider.user/user
at com.example.contentprovider.implement.insert(implement.java:138)
还有一些......
帮帮我......
答案 0 :(得分:0)
SQLiteConstraintException
这意味着您违反了数据库约束(例如外键)。我们无法从您发布的数据中知道哪一个。
答案 1 :(得分:0)
我认为contentvalues没有password
的键值对,而你明确表示密码在oncreate中不应为null。这可能是原因