Android - 尝试在内容提供商中添加值时失败

时间:2013-08-30 03:39:59

标签: android android-contentprovider

我正在尝试将值放在内容提供程序中,但应用程序将始终停止工作,返回trows:添加失败:“+ uri。从方法public Uri insert。     包com.amf.ContentProviders;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class ParcoursProvider extends ContentProvider 
{
    public static final String PROVIDER_NAME = 
        "com.amf.provider.Parcours";

    public static final Uri CONTENT_URI = 
        Uri.parse("content://"+ PROVIDER_NAME + "/parcours");

    public static final String _ID = "_id";
    public static final String NORMALE = "normale";

    private static final UriMatcher uriMatcher;
    static{
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "normale", 1);
    }

    //---for database use---
    private SQLiteDatabase parcoursDB;
    private static final String DATABASE_NAME = "Parcours";
    private static final String DATABASE_TABLE = "normale";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_CREATE =
            "create table " + DATABASE_TABLE + 
            " (_id integer primary key autoincrement, "
            + "normale REAL not null);";


    /* Classe helper pour la BD provisoire utilisees
     * pour le stockage des livres
     */

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) {
            Log.w("Content provider database", 
                  "maj de la BD de la version " + 
                  oldVersion + " à " + newVersion + 
                  ", ce qui détruira les anciennes données.");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    } 

    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)){
            //---get all books---
            case 1:
                return "normale ";   // vnd : vendor 
            //---get a particular book---
            case 2:                
                return "vnd.android.cursor.item/vnd.amf.parcours ";
            default:
                throw new IllegalArgumentException("URI invalide: " + uri);        
        } 
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //---add a new book---
        long rowID = parcoursDB.insert(DATABASE_TABLE,  "", values);

        //---if added successfully---
        if (rowID>0)
        {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
            getContext().getContentResolver().notifyChange(_uri, null);    
            return _uri;                
        }        
        throw new SQLException("Adding failed: " + uri);
    }

    @Override
    public boolean onCreate() {
        Context context = getContext();
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        parcoursDB = dbHelper.getWritableDatabase();
        return (parcoursDB == null)? false:true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
        sqlBuilder.setTables(DATABASE_TABLE);

        if (uriMatcher.match(uri) == 2)
            //---if getting a particular book---
            sqlBuilder.appendWhere(     // ajouter une clause where à la requête
                _ID + " = " + uri.getPathSegments().get(1));                

        if (sortOrder==null || sortOrder=="")
            sortOrder = NORMALE;

        Cursor c = sqlBuilder.query(
                parcoursDB, 
                projection, 
                selection, 
                selectionArgs, 
                null, 
                null, 
                sortOrder);

        //---register to watch a content URI for changes---
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int count = 0;
        switch (uriMatcher.match(uri)){
            case 1:
                count = parcoursDB.update(DATABASE_TABLE, values,selection, selectionArgs);
                break;
            case 2:                
                count = parcoursDB.update(DATABASE_TABLE, values,_ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection)), selectionArgs);
                break;
            default: throw new IllegalArgumentException("Unknown URI " + uri);    
        }       
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        // TODO Auto-generated method stub
        return 0;
    }
}

要添加的EditText:

        ContentValues values = new ContentValues();
        values.put("normale", ((EditText) findViewById(R.id.txtTitle)).getText().toString());
        Uri uri = getContentResolver().insert(Uri.parse("content://com.amf.provider.Parcours/normale"), values);

一如既往,一定非常简单。任何帮助,将不胜感激 先感谢您! :)

1 个答案:

答案 0 :(得分:0)

我不知道你为什么在这样的插入方法中添加一个大错误的代码。 throw new SQLException("Adding failed: " + uri);每次插入时都抛出异常,使每个插入查询都关闭应用程序。只需将其从代码中删除即可!