Android内容提供商无法更新数据

时间:2016-05-28 02:12:25

标签: android android-sqlite android-contentprovider

在我工作的项目中,我必须在运行时更新数据。但是在这里我想要更新。数据没有在数据库中更新。我正在使用Contentprovider进行插入/更新/删除操作

这是我的代码:

private void saveData()
    {
        String name1 = name.getText().toString();
        String phone  = mobile.getText().toString();
        String address = city.getText().toString();
        if (TextUtils.isEmpty(name1)||TextUtils.isEmpty(phone)||TextUtils.isEmpty(address))
        {
            if (TextUtils.isEmpty(name1))
            {
                name.setError("Feild can not be Empty");
            }
           else if (TextUtils.isEmpty(phone))
            {
                mobile.setError("Feild can not be Empty");
            }
           else if (TextUtils.isEmpty(address))
            {
                city.setError("Feild can not be Empty");
            }
        }
        else
        {
            ContentValues values = new ContentValues();
            values.put(DataProvider.Pname,name1);
            values.put(DataProvider.Pphone,phone);
            values.put(DataProvider.Paddress,address);
            values.put(DataProvider.PCategory,"Debt");
            values.put(DataProvider.Pamount,totamount);                       
            int a = getContentResolver().update(DataProvider.ContentUri_Person,values,DataProvider.PID+"=?",new String[]{String.valueOf(logid)});
            Toast.makeText(AddLog.this, String.valueOf(a)+"Value updated", Toast.LENGTH_SHORT).show();
            startActivity(returnback);
            finish();
        }
    }

内容提供商类

public class DataProvider extends ContentProvider
{
    static final String ProviderName = "com.example.mrudu.accounts.provider";
    static final String URLPerson = "content://"+ProviderName+"/Person_Detail";
    static final String URLDetails = "content://"+ProviderName+"/Details";
    static final Uri ContentUri_Person = Uri.parse(URLPerson);
    static final Uri ContentUri_Details = Uri.parse(URLDetails);

    //Tables
    private static String PTableName = "Person_Detail";
    private static String DTableName = "Details";

    public static long insertId = 0;

    //Person_Detail Coloumns
    public static String PID = "_id";
    public static String Pname = "PName";
    public static String Pphone = "PMobile";
    public static String Paddress = "PCity";
    public static String PCategory = "PCategory";
    public static String Pamount = "PAmount";

    //Details coloumn
    public static String DID = "_id";
    public static String Dname = "DName";
    public static String Damount = "DAmount";
    public static String Ddate = "DDate";
    public static String DCategory = "DCategory";
    public static String Per_In = "Per_In";

    private static final int Person = 1;
    private static final int Person_ID = 2;
    private static final int Details = 3;
    private static final int Details_Id = 4;

    static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static
    {
        uriMatcher.addURI(ProviderName,PTableName,Person);
        uriMatcher.addURI(ProviderName,PTableName+"/#",Person_ID);
        uriMatcher.addURI(ProviderName,DTableName,Details);
        uriMatcher.addURI(ProviderName,DTableName+"/#",Details_Id);
    }

    public static SQLiteDatabase sqLiteDatabase;
    private static String Databasename = "Accounts";
    private static int DatabaseVersion = 1;

    private class DatabaseHelper extends SQLiteOpenHelper
    {

        public DatabaseHelper(Context context)
        {
            super(context, Databasename, null, DatabaseVersion);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase)
        {
            String Create_Person = " Create Table "+PTableName+"("+PID+" INTEGER PRIMARYKEY ,"+Pname+" TEXT ,"+Pphone+" TEXT ,"+Paddress+" TEXT ,"+PCategory+" TEXT ,"+Pamount+" REAL"+")";
            String Create_Details = " Create Table "+DTableName+"("+DID+" INTEGER PRIMARYKEY ,"+Dname+" TEXT ,"+DCategory+" TEXT ,"+Damount+" REAl ,"+Ddate+" TEXT ,"+Per_In+" INTEGER )";
            sqLiteDatabase.execSQL(Create_Person);
            sqLiteDatabase.execSQL(Create_Details);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
        {
            sqLiteDatabase.execSQL("Drop TABLE if exists"+PTableName);
            sqLiteDatabase.execSQL("Drop TABLE if exists"+DTableName);
            onCreate(sqLiteDatabase);
        }
    }
    @Override
    public boolean onCreate()
    {
        Context context = getContext();
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        sqLiteDatabase = databaseHelper.getWritableDatabase();
        return (sqLiteDatabase==null)?false:true;
    }
    @Override
    public Uri insert(Uri uri, ContentValues values)
    {
        switch (uriMatcher.match(uri))
        {
            case Person:
                long rowId = sqLiteDatabase.insert(PTableName,null,values);
                insertId = rowId;
                if (rowId>0)
                {
                   Uri _uri = ContentUris.withAppendedId(ContentUri_Person,rowId);
                    getContext().getContentResolver().notifyChange(_uri,null);
                    return _uri;
                }
                break;
            case Details:
                long rowId1 = sqLiteDatabase.insert(DTableName,null,values);
                if (rowId1>0)
                {
                    Uri _uri = ContentUris.withAppendedId(ContentUri_Details,rowId1);
                    getContext().getContentResolver().notifyChange(_uri,null);
                    return _uri;
                }
                break;
        }
        return null;
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder)
    {
        SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();

        switch (uriMatcher.match(uri))
        {
            case Person_ID:
                sqLiteQueryBuilder.setTables(PTableName);
                sqLiteQueryBuilder.appendWhere(PID+ "="+ uri.getPathSegments().get(1));
                break;
            case Person:
                sqLiteQueryBuilder.setTables(PTableName);
                break;
            case Details_Id:
                sqLiteQueryBuilder.setTables(DTableName);
                sqLiteQueryBuilder.appendWhere(Per_In +"="+ uri.getPathSegments().get(1));
                break;
            case Details:
                sqLiteQueryBuilder.setTables(DTableName);
                break;
            default:
                throw new UnsupportedOperationException("Not yet implemented");
        }
        Cursor cursor = sqLiteQueryBuilder.query(sqLiteDatabase,projection,selection,selectionArgs,null,null,sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(),uri);
        return cursor;

    }
    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs)
    {
        int count = 0;
        switch (uriMatcher.match(uri))
        {
            case Person:
                count = sqLiteDatabase.update(PTableName,values,selection,selectionArgs);
                Log.d("Update","Person");
                break;
            case Person_ID:
                count = sqLiteDatabase.update(PTableName,values,PID+" = "+uri.getPathSegments().get(1)+(!TextUtils.isEmpty(selection)?" AND (" + selection + ')':""),selectionArgs);
                Log.d("Update","Person_Id");
                break;
            case Details:
                count = sqLiteDatabase.update(DTableName,values,selection,selectionArgs);
                Log.d("Update","Details");
                break;
            case Details_Id:
                count = sqLiteDatabase.update(DTableName,values,Per_In+" = "+uri.getPathSegments().get(1)+(!TextUtils.isEmpty(selection)?" AND (" + selection + ')':""),selectionArgs);
                Log.d("Update","Details_Id");
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri );
        }
        getContext().getContentResolver().notifyChange(uri,null);
        return count;
    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

您可以检查两个可能的问题。首先,您可以在CRUD操作开始时初始化SQLiteDatabase:

SQLiteDatabase sqlDB = dbHelper.getWritableDatabase();

第二个 - 使用SQLiteQueryBuilder并检查查询字符串。在那里,您可能在SQL查询中遇到问题。