0

我正在开发的应用程序成功地解析了 .xml 文件中的数据并将其存储到数据库中。但是,在每次应用程序启动时,数据库的大小都会随着存储解析数据的表行数而增长。例如,如果解析的数据需要存储三行,那么每次后续应用程序启动都会将数据库的大小(以行数计)增加到六、九、十二,依此类推。

我想要的是防止表在每次应用程序启动时增加。我每次都尝试过重置数据库,但我不确定如何正确执行此操作,或者这是否是我应该寻求的解决方案。

我有一个完整显示数据库的视图:

public class QAView extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qa_view);

        TextView tv = (TextView) findViewById(R.id.tvData);
        QADatabase entries = new QADatabase(this);
        try
        {
            entries.open();
            String data = entries.getAllData();
            tv.setText(data);
            entries.close();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
}

我有处理来自 .xml 文件的数据的解析器代码:

    XmlResourceParser parser = getResources().getXml(R.xml.qa);

    try
    {
        while(parser.next() != XmlPullParser.END_DOCUMENT)
        {
            if(parser.getEventType() != XmlPullParser.START_TAG)
            {
                continue;
            }

            String name = parser.getName();

            if(name.equals("question"))
            {
                question = readTextQuestion(parser);
            }
            else if(name.equals("options"))
            {
                readTextAnswers(parser);
            }
        }
    }
    catch(XmlPullParserException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

我也有相应的数据库代码:

public class QADatabase
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_QUESTION = "question";
    public static final String KEY_ANSWER1 = "answer1";
    public static final String KEY_ANSWER2 = "answer2";
    public static final String KEY_ANSWER3 = "answer3";
    public static final String KEY_ANSWER4 = "answer4";
    public static final String KEY_ANSWER5 = "answer5";
    public static final String KEY_CORRECT = "correct";

    public static final String DATABASE_NAME = "QuestionAnswerDatabase";
    public static final String DATABASE_TABLE = "QuestionAnswerTable";
    public static final int DATABASE_VERSION = 2;

    private DatabaseHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

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

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase)
        {
            sqLiteDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY, " +
                    KEY_QUESTION + " TEXT NOT NULL, " +
                    KEY_ANSWER1 + " TEXT NOT NULL, " +
                    KEY_ANSWER2 + " TEXT NOT NULL, " +
                    KEY_ANSWER3 + " TEXT NOT NULL, " +
                    KEY_ANSWER4 + " TEXT NOT NULL, " +
                    KEY_ANSWER5 + " TEXT NOT NULL, " +
                    KEY_CORRECT + " TEXT NOT NULL);"
            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2)
        {
            sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(sqLiteDatabase);
        }
    }

    public QADatabase(Context c)
    {
        ourContext = c;
    }

    public QADatabase open() throws SQLException
    {
        ourHelper = new DatabaseHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        ourHelper.close();
    }

    public void createEntry(String question, String answer1, String answer2, String answer3, String answer4, String answer5, String correct)
    {
        ContentValues cv = new ContentValues();
        cv.put(KEY_QUESTION, question);
        cv.put(KEY_ANSWER1, answer1);
        cv.put(KEY_ANSWER2, answer2);
        cv.put(KEY_ANSWER3, answer3);
        cv.put(KEY_ANSWER4, answer4);
        cv.put(KEY_ANSWER5, answer5);
        cv.put(KEY_CORRECT, correct);
        ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getAllData()
    {
        String[] columns = new String[] {KEY_ROWID, KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        while(c.moveToNext())
        {
            result = result + c.getString(0) + " " + c.getString(1) + " " + c.getString(2) + " " + c.getString(3) + " " + c.getString(4) + " " + c.getString(5) + " " + c.getString(6) + " " + c.getString(7) + "\n";
        }

        return result;
    }

    public String getQuestion(long l) throws SQLException
    {
        String[] columns = new String[] {KEY_ROWID, KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);

        if(c != null)
        {
            // move the cursor to the first row
            c.moveToFirst();

            // look for ID "l" in the "Questions" column
            String question = c.getString(1);

            return question;
        }

        // otherwise, return null
        return null;
    }

    public String getAnswerChoice(long l, int answerChoiceNumber) throws SQLException
    {
        String[] columns = new String[] {KEY_ROWID, KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);

        if(c != null)
        {
            // move the cursor to the first row
            c.moveToFirst();

            // look for ID "l" in the "Answers" column
            String correctAnswer = c.getString(answerChoiceNumber + 2); // need to offset by 2 for the KEY_ROWID and KEY_QUESTION columns

            return correctAnswer;
        }

        // otherwise, return null
        return null;
    }

    public Cursor getAllTitles()
    {
        return ourDatabase.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT},
                null, null, null, null, null);
    }
4

1 回答 1

0

您可以为每个问题分配一个固定的主 ID (rowID),因此一次又一次地这样做只会给您重复的主键约束异常,您的表不会增长。

于 2013-08-10T02:22:09.993 回答