Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序无法访问数据库进行插入和更新_Java_Android_Sqlite_Simplecursoradapter - Fatal编程技术网

Java Android应用程序无法访问数据库进行插入和更新

Java Android应用程序无法访问数据库进行插入和更新,java,android,sqlite,simplecursoradapter,Java,Android,Sqlite,Simplecursoradapter,我正在为Android开发notes应用程序,当我单击save按钮时,它应该使用content provider方法insert将数据插入数据库,然后加载程序将自动加载数据,SimpleCorsorAdapter应该填充该数据。但是,单击“保存”后,不会显示任何内容。在我看来,应用程序无法访问数据库,因为我在单击“保存”按钮时获取了此日志 02-27 12:31:31.112: E/SQLiteLog(13232): (257) Open fd: 74, file: /data/data

我正在为Android开发notes应用程序,当我单击save按钮时,它应该使用content provider方法insert将数据插入数据库,然后加载程序将自动加载数据,SimpleCorsorAdapter应该填充该数据。但是,单击“保存”后,不会显示任何内容。在我看来,应用程序无法访问数据库,因为我在单击“保存”按钮时获取了此日志

    02-27 12:31:31.112: E/SQLiteLog(13232): (257) Open fd: 74, file: /data/data/com.project.android.notes/databases/notes.db-journal
02-27 12:31:31.112: E/SQLiteLog(13232): (257) Close fd: 74
02-27 12:31:31.112: E/SQLiteLog(13232): (257) Open fd: 74, file: /data/data/com.project.android.notes/databases/notes.db-journal
02-27 12:31:31.186: E/SQLiteLog(13232): (257) Close fd: 74
02-27 12:31:31.271: E/SQLiteLog(13232): (257) Open fd: 80, file: /data/data/com.project.android.notes/databases/notes.db-journal
02-27 12:31:31.271: E/SQLiteLog(13232): (257) Close fd: 80
02-27 12:31:31.907: E/MCA(13851): Here call up the service!
02-27 12:31:31.908: E/MCA(13851): LT passed!
02-27 12:31:34.392: E/(180): AudioCloseDumpPCMFile file== NULL
02-27 12:31:34.392: E/(180): AudioCloseDumpPCMFile file== NULL
内容提供商代码:

    public class NotesProvider extends ContentProvider{

    // database
    private static TodoDatabaseHelper mdatabaseHelper;


    // used for the UriMacher
    private static final int NOTES = 10; //in case an entire table were requested to be returned.
    private static final int NOTES_ID = 20; //in case a specific item with a specific uri needs to be returned.




    //Let's build the UriMatcher:
    static UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); //sUriMatcher: s stands for static, it is just a name.
    //when we initialize the UriMatcher we first say that it still has no matches. Let's assign the matches:
    //we do that in a static block, that's once the class is created the matches are built.
    static {

        sUriMatcher.addURI(NotesContract.CONTENT_AUTHORITY, NotesContract.NOTES_PATH, NOTES); //match an entire table.
        sUriMatcher.addURI(NotesContract.CONTENT_AUTHORITY, NotesContract.NOTES_PATH +"/#", NOTES_ID);


    }//end matching static block.


    @Override
    public boolean onCreate() {
        //get a dbHelper obj,
         mdatabaseHelper = new TodoDatabaseHelper(getContext()); //why? Because this will help us retrieve a readable/writable db on which we can make operations like CRUD.
        return true; //we were successfully able to create an obj representation of the DB.
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        //here we should use the uri matcher to know how we can conduct the query.
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(NotesContract.NotesTable.TABLE_TODO); //could be used for joins also.

        verifyColumns (projection); //verify if the columns are valid.
        int match= sUriMatcher.match(uri); //save the int associated with this uri in a var to be able to use it in the switch.
        Cursor cursor=null; //initialize cursor, it will be used to return the result of the query.
        SQLiteDatabase  db= mdatabaseHelper.getReadableDatabase(); //we need this because we use an SQLiteQueryBuilder.
        switch (match) //start the switch
        {
            case NOTES:
                // call method query and send the params.
                cursor= queryBuilder.query(db,  projection, selection,
                       selectionArgs, null, null, sortOrder);
                break; //don't go to next case.
            case NOTES_ID:
                //first initialize the where clause in the query builder:
                queryBuilder.appendWhere(NotesContract.NotesTable._ID + "=" + ContentUris.parseId(uri));
                //invoke the query method:
                cursor= queryBuilder.query(db,  projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);

        }//end switch
        /*the queries above could be built using mSQLiteHelper.getReadableDatabase.query(..)*/
        cursor.setNotificationUri(getContext().getContentResolver(), uri); //very important and required, to update the ui when the data change.
        return cursor; //return the query result.
    }//end query.

    @Override
    public String getType(Uri uri) {

        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        int match = sUriMatcher.match(uri);
        SQLiteDatabase db = mdatabaseHelper.getWritableDatabase();
        long id = 0;
        switch (match) {
            case NOTES:
                id = db.insert(NotesContract.NotesTable.TABLE_TODO, null, contentValues);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null); //notify observers about any update.
        return Uri.parse(NotesContract.NOTES_PATH + "/" + id);

}

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int match = sUriMatcher.match(uri);
        SQLiteDatabase db = mdatabaseHelper.getWritableDatabase();
        int rowsDeleted = 0;

        switch (match) {
            case NOTES:
                rowsDeleted = db.delete(NotesContract.NotesTable.TABLE_TODO, selection, selectionArgs);
                break;
            case NOTES_ID:
                if (selection.isEmpty()){
                String whereClause= NotesContract.NotesTable._ID +  "=" + ContentUris.parseId(uri);
                rowsDeleted = db.delete(NotesContract.NotesTable.TABLE_TODO, whereClause, null);}

                else{
                    rowsDeleted = db.delete(NotesContract.NotesTable.TABLE_TODO,
                            NotesContract.NotesTable._ID + "=" + ContentUris.parseId(uri)
                                    + " and " + selection,
                            selectionArgs);
                }//end else
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }//end switch
        getContext().getContentResolver().notifyChange(uri, null); //if any delete happens notify the observers.
        return rowsDeleted;
    }//end delete


    @Override
    public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mdatabaseHelper.getWritableDatabase(); //return a writable obj from the db.
        int rowsUpdated = 0;
        int match= sUriMatcher.match(uri);
        switch (match) {
            case NOTES:
                rowsUpdated = db.update(NotesContract.NotesTable.TABLE_TODO,
                        contentValues,
                        selection,
                        selectionArgs);
                break;
            case NOTES_ID:
                long id = ContentUris.parseId(uri);
                if (TextUtils.isEmpty(selection)) {
                    rowsUpdated = db.update(NotesContract.NotesTable.TABLE_TODO,
                            contentValues,
                            NotesContract.NotesTable._ID + "=" + id,
                            null);
                } else {
                    rowsUpdated = db.update(NotesContract.NotesTable.TABLE_TODO,
                            contentValues,
                            NotesContract.NotesTable._ID + "=" + id
                                    + " and "
                                    + selection,
                            selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsUpdated;
    }

    private void verifyColumns (String [] cols) {
        //lets get the columns in the Db and compare.
        String[] existed = {NotesContract.NotesTable._ID, NotesContract.NotesTable.COLUMN_CATEGORY,
                NotesContract.NotesTable.COLUMN_DESCRIPTION,
                NotesContract.NotesTable.COLUMN_SUMMARY};
        if (cols != null) {
            ArrayList<String> existedCols = new ArrayList<String>(Arrays.asList(existed)); //this is how we convert an array to an arraylist.
            ArrayList<String> requestedCols = new ArrayList<String>(Arrays.asList(cols));
            // check if all columns which are requested are available
            if (!existedCols.containsAll(requestedCols)) {
                throw new IllegalArgumentException("Some columns don't exist in the database");
            }//end if.
        }//end outer if.
    }//end verification.

    public static Cursor searchDescription (String key)
    {
        String desc []= {"%"+key +"%"};
        SQLiteDatabase db = mdatabaseHelper.getReadableDatabase();
        Cursor rtCursor= db.rawQuery("select * from " + NotesContract.NotesTable.TABLE_TODO+ " where " +
                NotesContract.NotesTable.COLUMN_DESCRIPTION + " like ?", desc);

        return rtCursor;
    }//end searchDescription

}//end Provider.
有人能告诉我为什么会发生这种情况,以及如何解决这个问题吗

谢谢

将此用于插入

 public void onClickAddName(View view) {
        ContentValues values = new ContentValues();
        values.put(MyProvider.name, ((EditText) findViewById(R.id.txtName))
                .getText().toString());
        Uri uri = getContentResolver().insert(MyProvider.CONTENT_URI, values);
        Toast.makeText(getBaseContext(), "New record inserted", Toast.LENGTH_LONG).show();
    }

你能发布你的代码吗?谢谢你的评论,我更新了问题谢谢你的回答,我在详细的活动中做了类似的事情,但它不起作用。您能告诉我我犯了什么错误吗?完成后,我只将内容提供程序代码和执行插入查询的类的代码放在您定义此id的位置=db.insertNotesContract.NotesTable.TABLE_TODO,null,contentValues;这里什么是空的?当我检查不同的教程时,我发现它通常设置为空。给我你的邮件id我想发送一个小应用。?
 public void onClickAddName(View view) {
        ContentValues values = new ContentValues();
        values.put(MyProvider.name, ((EditText) findViewById(R.id.txtName))
                .getText().toString());
        Uri uri = getContentResolver().insert(MyProvider.CONTENT_URI, values);
        Toast.makeText(getBaseContext(), "New record inserted", Toast.LENGTH_LONG).show();
    }