4

我有一个在 java 程序中填充的 sqlite 数据库(我手动添加了语言环境,以便它可以与 Android 一起使用)。数据库中的每个条目本质上或多或少都是一个句子,我将所有句子加起来组成一章。

问题:有时,(到目前为止,这仅在 2 部冰淇淋三明治手机上发现)我看到部分句子被写入,然后是换行符,然后是部分句子的重复。从数据库中获取句子后,我用一个相当大的算法解析句子,但这似乎不是问题,因为在我卸载应用程序(破坏缓存)并重新加载同一章节后,问题就消失了。差异似乎来自我从数据库中获得的信息。

用户看到的示例:

小男孩收到了礼物,要换 行,因为某种原因, 男孩收到了礼物,要送给他的小妹妹作为生日礼物。

一些可能很奇怪的事情:数据库在 sentence_num(一章中每个句子的唯一编号)、book_name、chample_num 之间有一个 3 列 PK。

我用来访问数据库的代码

    public synchronized List<ChapterElement> getSentenceChapter(SentenceReference sentenceRef) {
        Book book = sentenceRef.getBook();
        Integer chapter = sentenceRef.getChapter();

        String[] columns = {
                SentenceDatabaseHelper.COLUMN_SENTENCE_NUM,
                SentenceDatabaseHelper.COLUMN_SENTENCE_IS_START_OF_PARAGRAPH,
                SentenceDatabaseHelper.COLUMN_SENTENCE_HAS_FOOT_NOTE,
                SentenceDatabaseHelper.COLUMN_SENTENCE_HAS_CROSS_REFERENCE,
                SentenceDatabaseHelper.COLUMN_SENTENCE_CONTENT
        };

        String selection = SentenceDatabaseHelper.COLUMN_SENTENCE_BOOK + "=? AND " 
                + SentenceDatabaseHelper.COLUMN_SENTENCE_CHAPTER + "=?";

        String[] selectionArgs = {book.getShortenedName(), chapter.toString()};

        String orderBy = SentenceDatabaseHelper.COLUMN_SENTENCE_ORDER_NUM + " ASC";

        Cursor cursor = null;
        SQLiteDatabase database = null;

        try {
            database = sentenceDBH.getReadableDatabase();
            cursor = database.query(SentenceDatabaseHelper.TABLE_SENTENCE,
                    columns, selection, selectionArgs, null, null, orderBy);
        } catch (Exception e) {
            Log.e(TAG, "There was an error while accessing the database.", e);
            if (cursor != null) {
                cursor.close();
            }
            if (database != null) {
                database.close();
            }
        }

        int sentenceNumIndex = cursor.getColumnIndex(SentenceDatabaseHelper.COLUMN_SENTENCE_NUM);
        int hasFootNoteIndex = cursor.getColumnIndex(SentenceDatabaseHelper.COLUMN_SENTENCE_HAS_FOOT_NOTE);
        int hasCrossReferenceIndex = cursor.getColumnIndex(SentenceDatabaseHelper.COLUMN_SENTENCE_HAS_CROSS_REFERENCE);
        int isStartOfParagraphIndex = cursor.getColumnIndex(SentenceDatabaseHelper.COLUMN_SENTENCE_IS_START_OF_PARAGRAPH);
        int contentIndex = cursor.getColumnIndex(SentenceDatabaseHelper.COLUMN_SENTENCE_CONTENT);

        List<ChapterElement> chapterElements = new ArrayList<ChapterElement>( cursor.getCount() );
        while ( cursor.moveToNext() ) {
            int sentenceNum = cursor.getInt(sentenceNumIndex);
            boolean hasFootNote = (cursor.getInt(hasFootNoteIndex) == 1) ? true : false;
            boolean hasCrossRef = (cursor.getInt(hasCrossReferenceIndex) == 1) ? true : false;
            boolean isStartOfParagraph = (cursor.getInt(isStartOfParagraphIndex) == 1) ? true : false;
            String content = cursor.getString(contentIndex);

            ChapterElement chapterElement = new ChapterElement(sentenceNum, hasFootNote, hasCrossRef,
                    isStartOfParagraph, content);
            chapterElements.add(chapterElement);
        }

        cursor.close();
        database.close();

        return chapterElements;
    }

任何帮助,将不胜感激。

4

0 回答 0